Skip to content

Commit 6597c80

Browse files
fix(kubernetes_multicluster): pin Is* methods to exact Kubernetes API groups
1 parent 5df1ef3 commit 6597c80

3 files changed

Lines changed: 21 additions & 57 deletions

File tree

pkg/app/pipedv1/plugin/kubernetes_multicluster/config/application.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ type KubernetesDeploymentInput struct {
109109
// Default is false.
110110
AutoCreateNamespace bool `json:"autoCreateNamespace,omitempty"`
111111

112-
// TODO: Define fields for KubernetesDeploymentInput.
113-
114112
MultiTargets []KubernetesMultiTarget `json:"multiTargets,omitempty"`
115113
}
116114

pkg/app/pipedv1/plugin/kubernetes_multicluster/provider/diff_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ func TestDiff(t *testing.T) {
3838
}{
3939
{
4040
name: "Secret no diff 1",
41-
manifests: `apiVersion: apps/v1
41+
manifests: `apiVersion: v1
4242
kind: Secret
4343
metadata:
4444
name: secret-management
4545
---
46-
apiVersion: apps/v1
46+
apiVersion: v1
4747
kind: Secret
4848
metadata:
4949
name: secret-management
@@ -53,7 +53,7 @@ metadata:
5353
},
5454
{
5555
name: "Secret no diff 2",
56-
manifests: `apiVersion: apps/v1
56+
manifests: `apiVersion: v1
5757
kind: Secret
5858
metadata:
5959
name: secret-management
@@ -62,7 +62,7 @@ data:
6262
stringData:
6363
foo: bar
6464
---
65-
apiVersion: apps/v1
65+
apiVersion: v1
6666
kind: Secret
6767
metadata:
6868
name: secret-management
@@ -76,15 +76,15 @@ stringData:
7676
},
7777
{
7878
name: "Secret no diff with merge",
79-
manifests: `apiVersion: apps/v1
79+
manifests: `apiVersion: v1
8080
kind: Secret
8181
metadata:
8282
name: secret-management
8383
data:
8484
password: hoge
8585
foo: YmFy
8686
---
87-
apiVersion: apps/v1
87+
apiVersion: v1
8888
kind: Secret
8989
metadata:
9090
name: secret-management
@@ -98,15 +98,15 @@ stringData:
9898
},
9999
{
100100
name: "Secret no diff override false-positive",
101-
manifests: `apiVersion: apps/v1
101+
manifests: `apiVersion: v1
102102
kind: Secret
103103
metadata:
104104
name: secret-management
105105
data:
106106
password: hoge
107107
foo: YmFy
108108
---
109-
apiVersion: apps/v1
109+
apiVersion: v1
110110
kind: Secret
111111
metadata:
112112
name: secret-management
@@ -122,14 +122,14 @@ stringData:
122122
},
123123
{
124124
name: "Secret has diff",
125-
manifests: `apiVersion: apps/v1
125+
manifests: `apiVersion: v1
126126
kind: Secret
127127
metadata:
128128
name: secret-management
129129
data:
130130
foo: YmFy
131131
---
132-
apiVersion: apps/v1
132+
apiVersion: v1
133133
kind: Secret
134134
metadata:
135135
name: secret-management

pkg/app/pipedv1/plugin/kubernetes_multicluster/provider/manifest.go

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,6 @@ import (
2525
sdk "github.com/pipe-cd/piped-plugin-sdk-go"
2626
)
2727

28-
var builtinAPIGroups = map[string]struct{}{
29-
"admissionregistration.k8s.io": {},
30-
"apiextensions.k8s.io": {},
31-
"apiregistration.k8s.io": {},
32-
"apps": {},
33-
"authentication.k8s.io": {},
34-
"authorization.k8s.io": {},
35-
"autoscaling": {},
36-
"batch": {},
37-
"certificates.k8s.io": {},
38-
"coordination.k8s.io": {},
39-
"extensions": {},
40-
"internal.autoscaling.k8s.io": {},
41-
"metrics.k8s.io": {},
42-
"networking.k8s.io": {},
43-
"node.k8s.io": {},
44-
"policy": {},
45-
"rbac.authorization.k8s.io": {},
46-
"scheduling.k8s.io": {},
47-
"storage.k8s.io": {},
48-
"": {},
49-
}
50-
51-
func isBuiltinAPIGroup(apiGroup string) bool {
52-
_, ok := builtinAPIGroups[apiGroup]
53-
return ok
54-
}
5528

5629
// Manifest represents a Kubernetes resource manifest.
5730
type Manifest struct {
@@ -121,13 +94,12 @@ func (m Manifest) Name() string {
12194
// IsWorkload returns true if the manifest is a Deployment, StatefulSet, DaemonSet, ReplicaSet, or Pod.
12295
// It checks the API group and the kind of the manifest.
12396
func (m Manifest) IsWorkload() bool {
124-
// TODO: check the API group more strictly.
125-
if !isBuiltinAPIGroup(m.body.GroupVersionKind().Group) {
126-
return false
127-
}
97+
group := m.body.GroupVersionKind().Group
12898
switch m.body.GetKind() {
129-
case KindDeployment, KindStatefulSet, KindDaemonSet, KindReplicaSet, KindPod:
130-
return true
99+
case KindDeployment, KindStatefulSet, KindDaemonSet, KindReplicaSet:
100+
return group == "apps"
101+
case KindPod:
102+
return group == ""
131103
default:
132104
return false
133105
}
@@ -136,43 +108,37 @@ func (m Manifest) IsWorkload() bool {
136108
// IsService returns true if the manifest is a Service.
137109
// It checks the API group and the kind of the manifest.
138110
func (m Manifest) IsService() bool {
139-
// TODO: check the API group more strictly.
140-
return isBuiltinAPIGroup(m.body.GroupVersionKind().Group) && m.body.GetKind() == KindService
111+
return m.body.GroupVersionKind().Group == "" && m.body.GetKind() == KindService
141112
}
142113

143114
// IsDeployment returns true if the manifest is a Deployment.
144115
// It checks the API group and the kind of the manifest.
145116
func (m Manifest) IsDeployment() bool {
146-
// TODO: check the API group more strictly.
147-
return isBuiltinAPIGroup(m.body.GroupVersionKind().Group) && m.body.GetKind() == KindDeployment
117+
return m.body.GroupVersionKind().Group == "apps" && m.body.GetKind() == KindDeployment
148118
}
149119

150120
// IsStatefulSet returns true if the manifest is a StatefulSet.
151121
// It checks the API group and the kind of the manifest.
152122
func (m Manifest) IsStatefulSet() bool {
153-
// TODO: check the API group more strictly.
154-
return isBuiltinAPIGroup(m.body.GroupVersionKind().Group) && m.body.GetKind() == KindStatefulSet
123+
return m.body.GroupVersionKind().Group == "apps" && m.body.GetKind() == KindStatefulSet
155124
}
156125

157126
// IsDaemonSet returns true if the manifest is a DaemonSet.
158127
// It checks the API group and the kind of the manifest.
159128
func (m Manifest) IsDaemonSet() bool {
160-
// TODO: check the API group more strictly.
161-
return isBuiltinAPIGroup(m.body.GroupVersionKind().Group) && m.body.GetKind() == KindDaemonSet
129+
return m.body.GroupVersionKind().Group == "apps" && m.body.GetKind() == KindDaemonSet
162130
}
163131

164132
// IsSecret returns true if the manifest is a Secret.
165133
// It checks the API group and the kind of the manifest.
166134
func (m Manifest) IsSecret() bool {
167-
// TODO: check the API group more strictly.
168-
return isBuiltinAPIGroup(m.body.GroupVersionKind().Group) && m.body.GetKind() == KindSecret
135+
return m.body.GroupVersionKind().Group == "" && m.body.GetKind() == KindSecret
169136
}
170137

171138
// IsConfigMap returns true if the manifest is a ConfigMap.
172139
// It checks the API group and the kind of the manifest.
173140
func (m Manifest) IsConfigMap() bool {
174-
// TODO: check the API group more strictly.
175-
return isBuiltinAPIGroup(m.body.GroupVersionKind().Group) && m.body.GetKind() == KindConfigMap
141+
return m.body.GroupVersionKind().Group == "" && m.body.GetKind() == KindConfigMap
176142
}
177143

178144
// UnmarshalJSON implements the json.Unmarshaler interface.

0 commit comments

Comments
 (0)