Skip to content

Commit b826331

Browse files
mprahlopenshift-merge-robot
authored andcommitted
Use a predelete hook to cleanly uninstall the config-policy-controller
This change makes it so that a finalizer is added to the config-policy-controller ManagedClusterAddOn object. Now when it is deleted, it will trigger an additional ManifestWork to create a Pod which signals to the config-policy-controller that it's being uninstalled and it waits for all ConfigurationPolicy objects to have their finalizers removed before exiting. Once the Pod exits successfully, the finalizer on the ManagedClusterAddOn object is removed and the config-policy-controller uninstall proceeds. If there are a lot of policies, it may take longer than 30 seconds, so the grace period was bumped up to 120 seconds to exit cleanly. Note that now with KIND_VERSION set to minimum, it uses a newer version for the Hub to match the lowest supported version for the Hub. This is to work around this: open-cluster-management-io/api#206 Signed-off-by: mprahl <[email protected]>
1 parent 66ec7d9 commit b826331

File tree

8 files changed

+102
-26
lines changed

8 files changed

+102
-26
lines changed

build/manage-clusters.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ if [[ -n "${MANAGED_CLUSTER_COUNT//[0-9]}" ]] || [[ "${MANAGED_CLUSTER_COUNT}" =
1313
exit 1
1414
fi
1515

16+
HUB_KIND_VERSION=$KIND_VERSION
17+
if [[ "${KIND_VERSION}" == "minimum" ]]; then
18+
# The hub supports less Kubernetes versions than the managed cluster.
19+
HUB_KIND_VERSION=v1.23.13
20+
fi
21+
1622
KIND_PREFIX=${KIND_PREFIX:-"policy-addon-ctrl"}
1723
CLUSTER_PREFIX=${CLUSTER_PREFIX:-"cluster"}
1824

@@ -27,10 +33,10 @@ case ${RUN_MODE} in
2733
make e2e-debug
2834
;;
2935
create)
30-
make kind-deploy-controller
36+
KIND_VERSION=$HUB_KIND_VERSION make kind-deploy-controller
3137
;;
3238
create-dev)
33-
make kind-prep-ocm
39+
KIND_VERSION=$HUB_KIND_VERSION make kind-prep-ocm
3440
;;
3541
deploy-addons)
3642
make kind-deploy-addons-hub

go.mod

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ require (
1212
k8s.io/apimachinery v0.25.0
1313
k8s.io/client-go v0.25.0
1414
k8s.io/component-base v0.25.0
15-
open-cluster-management.io/addon-framework v0.5.1-0.20221206033210-55b40b1bc8c6
16-
open-cluster-management.io/api v0.8.1-0.20220919023232-a2688935edf3
15+
open-cluster-management.io/addon-framework v0.6.0
16+
open-cluster-management.io/api v0.9.1-0.20221222015712-61cf30907d02
1717
sigs.k8s.io/controller-runtime v0.11.2
1818
)
1919

@@ -130,4 +130,6 @@ require (
130130
replace (
131131
golang.org/x/text => golang.org/x/text v0.3.8 // CVE-2022-32149
132132
helm.sh/helm/v3 => helm.sh/helm/v3 v3.9.4 // sonatype-2022-5277; see helm v3.9.4 release notes
133+
// Pending https://github.com/open-cluster-management-io/addon-framework/pull/149
134+
open-cluster-management.io/addon-framework => github.com/mprahl/addon-framework v0.0.0-20230213164346-e05945686992
133135
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,8 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
496496
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
497497
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
498498
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
499+
github.com/mprahl/addon-framework v0.0.0-20230213164346-e05945686992 h1:40OmIBncAOCO/IDAlRdI6zXmx/Yh3+2nMovd1kyhSvE=
500+
github.com/mprahl/addon-framework v0.0.0-20230213164346-e05945686992/go.mod h1:efcOXMAzJQR8jNtz9bS8YCbZ/Jr2fmgKJFOXeiAc+gk=
499501
github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
500502
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
501503
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
@@ -1267,10 +1269,8 @@ modernc.org/golex v1.0.0/go.mod h1:b/QX9oBD/LhixY6NDh+IdGv17hgB+51fET1i2kPSmvk=
12671269
modernc.org/mathutil v1.0.0/go.mod h1:wU0vUrJsVWBZ4P6e7xtFJEhFSNsfRLJ8H458uRjg03k=
12681270
modernc.org/strutil v1.0.0/go.mod h1:lstksw84oURvj9y3tn8lGvRxyRC1S2+g5uuIzNfIOBs=
12691271
modernc.org/xc v1.0.0/go.mod h1:mRNCo0bvLjGhHO9WsyuKVU4q0ceiDDDoEeWDJHrNx8I=
1270-
open-cluster-management.io/addon-framework v0.5.1-0.20221206033210-55b40b1bc8c6 h1:TnQDy3V7wkiblgR5tz1J+ASswcfnRB9gpVbmwCCecVo=
1271-
open-cluster-management.io/addon-framework v0.5.1-0.20221206033210-55b40b1bc8c6/go.mod h1:Fymctw1tnmCTXnmAMgc0zdHetAw6UaiAsj1S6w5VW6s=
1272-
open-cluster-management.io/api v0.8.1-0.20220919023232-a2688935edf3 h1:x1KFpqwnVbt4YWnpZzZeip+zWRUIMnsj0Bx7Y2Lrtk8=
1273-
open-cluster-management.io/api v0.8.1-0.20220919023232-a2688935edf3/go.mod h1:+OEARSAl2jIhuLItUcS30UgLA3khmA9ihygLVxzEn+U=
1272+
open-cluster-management.io/api v0.9.1-0.20221222015712-61cf30907d02 h1:QUO3HHm38/99Zr7+ZB3j6PoDpzRzFKOmBBXNMAgaKxs=
1273+
open-cluster-management.io/api v0.9.1-0.20221222015712-61cf30907d02/go.mod h1:6BB/Y6r3hXlPjpJgDwIs6Ubxyx/kXXOg6D9Cntg1I9E=
12741274
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
12751275
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
12761276
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

pkg/addon/common.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,17 @@ type PolicyAgentAddon struct {
156156
agent.AgentAddon
157157
}
158158

159-
func (pa *PolicyAgentAddon) Manifests(cluster *clusterv1.ManagedCluster,
159+
func (pa *PolicyAgentAddon) Manifests(
160+
cluster *clusterv1.ManagedCluster,
160161
addon *addonapiv1alpha1.ManagedClusterAddOn,
162+
hostingCluster *clusterv1.ManagedCluster,
161163
) ([]runtime.Object, error) {
162164
pauseAnnotation := addon.GetAnnotations()[PolicyAddonPauseAnnotation]
163165
if pauseAnnotation == "true" {
164166
return nil, errors.New("the Policy Addon controller is paused due to the policy-addon-pause annotation")
165167
}
166168

167-
return pa.AgentAddon.Manifests(cluster, addon)
169+
return pa.AgentAddon.Manifests(cluster, addon, hostingCluster)
168170
}
169171

170172
// getLogLevel verifies the user-provided log level against Zap, returning 0 if the check fails.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Copyright Contributors to the Open Cluster Management project
2+
3+
apiVersion: v1
4+
kind: Pod
5+
metadata:
6+
name: {{ include "controller.fullname" . }}-uninstall
7+
namespace: {{ .Release.Namespace }}
8+
labels:
9+
app: {{ include "controller.fullname" . }}
10+
chart: {{ include "controller.chart" . }}
11+
release: {{ .Release.Name }}
12+
heritage: {{ .Release.Service }}
13+
addon.open-cluster-management.io/hosted-manifest-location: hosting
14+
annotations:
15+
addon.open-cluster-management.io/addon-pre-delete: ""
16+
spec:
17+
restartPolicy: OnFailure
18+
containers:
19+
- name: {{ .Chart.Name }}-uninstall
20+
image: "{{ .Values.global.imageOverrides.config_policy_controller }}"
21+
imagePullPolicy: "{{ .Values.global.imagePullPolicy }}"
22+
command: ["config-policy-controller"]
23+
args:
24+
- trigger-uninstall
25+
- --deployment-name={{ include "controller.fullname" . }}
26+
- --deployment-namespace={{ .Release.Namespace }}
27+
{{- if eq .Values.installMode "Hosted" }}
28+
- --policy-namespace={{ .Release.Namespace }}
29+
{{- else }}
30+
- --policy-namespace={{ .Values.clusterName }}
31+
{{- end }}
32+
- --log-encoder={{ .Values.args.logEncoder }}
33+
- --log-level={{ .Values.args.logLevel }}
34+
- --v={{ .Values.args.pkgLogLevel }}
35+
env:
36+
{{- if .Values.global.proxyConfig }}
37+
- name: HTTP_PROXY
38+
value: {{ .Values.global.proxyConfig.HTTP_PROXY }}
39+
- name: HTTPS_PROXY
40+
value: {{ .Values.global.proxyConfig.HTTPS_PROXY }}
41+
- name: NO_PROXY
42+
value: {{ .Values.global.proxyConfig.NO_PROXY }}
43+
{{- end }}
44+
resources: {{- toYaml .Values.resources | nindent 10 }}
45+
securityContext:
46+
allowPrivilegeEscalation: false
47+
capabilities:
48+
drop:
49+
- ALL
50+
privileged: false
51+
readOnlyRootFilesystem: true
52+
{{- if .Values.global.imagePullSecret }}
53+
imagePullSecrets:
54+
- name: "{{ .Values.global.imagePullSecret }}"
55+
{{- end }}
56+
affinity: {{ toYaml .Values.affinity | nindent 8 }}
57+
{{- if hasKey .Values "tolerations" }}
58+
tolerations: {{ toYaml .Values.tolerations | nindent 8 }}
59+
{{- end }}
60+
{{- if hasKey .Values.global "nodeSelector" }}
61+
nodeSelector: {{ toYaml .Values.global.nodeSelector | nindent 8 }}
62+
{{- end }}
63+
hostNetwork: false
64+
hostPID: false
65+
hostIPC: false
66+
serviceAccount: {{ include "controller.serviceAccountName" . }}
67+
securityContext:
68+
runAsNonRoot: true

pkg/addon/configpolicy/manifests/managedclusterchart/templates/deployment.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ spec:
5757
imagePullPolicy: "{{ .Values.global.imagePullPolicy }}"
5858
command: ["config-policy-controller"]
5959
args:
60+
- controller
6061
- "--enable-lease=true"
6162
- "--cluster-name={{ .Values.clusterName }}"
6263
{{- if eq (.Values.replicas | int) 1 }}
@@ -177,3 +178,4 @@ spec:
177178
serviceAccount: {{ include "controller.serviceAccountName" . }}
178179
securityContext:
179180
runAsNonRoot: true
181+
terminationGracePeriodSeconds: 120

test/e2e/case1_framework_deployment_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,8 @@ var _ = Describe("Test framework deployment", func() {
457457
container, ok := containers[0].(map[string]interface{})
458458
Expect(ok).To(BeTrue())
459459

460-
if startupProbeInCluster(i) {
460+
// Use i+1 since the for loop ranges over a slice skipping first index
461+
if startupProbeInCluster(i + 1) {
461462
By(logPrefix + "checking for startupProbe on kubernetes 1.20 or higher")
462463
_, found, err = unstructured.NestedMap(container, "startupProbe")
463464
Expect(err).To(BeNil())

test/e2e/case2_config_deployment_test.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ var _ = Describe("Test config-policy-controller deployment", func() {
9898
"removing the config-policy-controller deployment when the ManagedClusterAddOn CR is removed")
9999
Kubectl("delete", "-n", cluster.clusterName, "-f", case2ManagedClusterAddOnCR)
100100
deploy := GetWithTimeout(
101-
cluster.clusterClient, gvrDeployment, case2DeploymentName, addonNamespace, false, 30,
101+
cluster.clusterClient, gvrDeployment, case2DeploymentName, addonNamespace, false, 180,
102102
)
103103
Expect(deploy).To(BeNil())
104104
}
@@ -142,7 +142,7 @@ var _ = Describe("Test config-policy-controller deployment", func() {
142142
"removing the config-policy-controller deployment when the ManagedClusterAddOn CR is removed")
143143
Kubectl("delete", "-n", cluster.clusterName, "-f", case2ManagedClusterAddOnCR)
144144
deploy = GetWithTimeout(
145-
cluster.clusterClient, gvrDeployment, case2DeploymentName, addonNamespace, false, 30,
145+
cluster.clusterClient, gvrDeployment, case2DeploymentName, addonNamespace, false, 180,
146146
)
147147
Expect(deploy).To(BeNil())
148148
}
@@ -154,7 +154,7 @@ var _ = Describe("Test config-policy-controller deployment", func() {
154154
})
155155

156156
It("should create the default config-policy-controller deployment in hosted mode", Label("hosted-mode"), func() {
157-
for i, cluster := range managedClusterList[1:] {
157+
for _, cluster := range managedClusterList[1:] {
158158
Expect(cluster.clusterType).To(Equal("managed"))
159159

160160
cluster = managedClusterConfig{
@@ -176,22 +176,17 @@ var _ = Describe("Test config-policy-controller deployment", func() {
176176
logPrefix, hubClient, case2ManagedClusterAddOnName,
177177
cluster.clusterName, managedClusterList[0].clusterName, installNamespace)
178178

179-
verifyConfigPolicyDeployment(logPrefix, hubClient, cluster.clusterName, installNamespace, i)
179+
verifyConfigPolicyDeployment(logPrefix, hubClient, cluster.clusterName, installNamespace, 0)
180180

181181
By(logPrefix +
182182
"removing the config-policy-controller deployment when the ManagedClusterAddOn CR is removed")
183-
err := hubClient.Resource(gvrSecret).Namespace(installNamespace).Delete(
184-
context.TODO(), "config-policy-controller-managed-kubeconfig", metav1.DeleteOptions{},
185-
)
186-
Expect(err).To(BeNil())
187-
188-
err = clientDynamic.Resource(gvrManagedClusterAddOn).Namespace(cluster.clusterName).Delete(
183+
err := clientDynamic.Resource(gvrManagedClusterAddOn).Namespace(cluster.clusterName).Delete(
189184
context.TODO(), case2ManagedClusterAddOnName, metav1.DeleteOptions{},
190185
)
191186
Expect(err).To(BeNil())
192187

193188
deploy := GetWithTimeout(
194-
hubClient, gvrDeployment, case2DeploymentName, installNamespace, false, 30,
189+
hubClient, gvrDeployment, case2DeploymentName, installNamespace, false, 180,
195190
)
196191
Expect(deploy).To(BeNil())
197192

@@ -207,7 +202,7 @@ var _ = Describe("Test config-policy-controller deployment", func() {
207202
By("Creating the config-policy-controller ClusterManagementAddOn to use the AddOnDeploymentConfig")
208203
Kubectl("apply", "-f", case2ClusterManagementAddOnCR)
209204

210-
for i, cluster := range managedClusterList[1:] {
205+
for _, cluster := range managedClusterList[1:] {
211206
Expect(cluster.clusterType).To(Equal("managed"))
212207

213208
cluster = managedClusterConfig{
@@ -229,7 +224,7 @@ var _ = Describe("Test config-policy-controller deployment", func() {
229224
logPrefix, hubClient, case2ManagedClusterAddOnName,
230225
cluster.clusterName, managedClusterList[0].clusterName, installNamespace)
231226

232-
verifyConfigPolicyDeployment(logPrefix, hubClient, cluster.clusterName, installNamespace, i)
227+
verifyConfigPolicyDeployment(logPrefix, hubClient, cluster.clusterName, installNamespace, 0)
233228

234229
By(logPrefix + "Removing the ManagedClusterAddOn CR")
235230
err := clientDynamic.Resource(gvrManagedClusterAddOn).Namespace(cluster.clusterName).Delete(
@@ -241,7 +236,7 @@ var _ = Describe("Test config-policy-controller deployment", func() {
241236
"Verifying controller deployment is removed when the ManagedClusterAddOn CR is removed")
242237

243238
deploy := GetWithTimeout(
244-
hubClient, gvrDeployment, case2DeploymentName, installNamespace, false, 30,
239+
hubClient, gvrDeployment, case2DeploymentName, installNamespace, false, 180,
245240
)
246241
Expect(deploy).To(BeNil())
247242

@@ -377,7 +372,7 @@ var _ = Describe("Test config-policy-controller deployment", func() {
377372
"removing the config-policy-controller deployment when the ManagedClusterAddOn CR is removed")
378373
Kubectl("delete", "-n", cluster.clusterName, "-f", case2ManagedClusterAddOnCR)
379374
deploy = GetWithTimeout(
380-
cluster.clusterClient, gvrDeployment, case2DeploymentName, addonNamespace, false, 30,
375+
cluster.clusterClient, gvrDeployment, case2DeploymentName, addonNamespace, false, 180,
381376
)
382377
Expect(deploy).To(BeNil())
383378
}

0 commit comments

Comments
 (0)