@@ -3,13 +3,13 @@ package install
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "strings"
6
7
7
8
"github.com/operator-framework/api/pkg/operators/v1alpha1"
8
9
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
9
10
10
11
log "github.com/sirupsen/logrus"
11
12
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
12
- k8serrors "k8s.io/apimachinery/pkg/api/errors"
13
13
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14
14
"k8s.io/apimachinery/pkg/labels"
15
15
)
@@ -69,80 +69,105 @@ func (i *StrategyDeploymentInstaller) createOrUpdateWebhook(caPEM []byte, desc v
69
69
}
70
70
71
71
func (i * StrategyDeploymentInstaller ) createOrUpdateMutatingWebhook (ogNamespacelabelSelector * metav1.LabelSelector , caPEM []byte , desc v1alpha1.WebhookDescription ) error {
72
- webhooks := []admissionregistrationv1.MutatingWebhook {
73
- desc .GetMutatingWebhook (i .owner .GetNamespace (), ogNamespacelabelSelector , caPEM ),
72
+ webhookSelector := labels .SelectorFromSet (ownerutil .OwnerLabel (i .owner , i .owner .GetObjectKind ().GroupVersionKind ().Kind )).String ()
73
+ existingWebhooks , err := i .strategyClient .GetOpClient ().KubernetesInterface ().AdmissionregistrationV1 ().MutatingWebhookConfigurations ().List (context .TODO (), metav1.ListOptions {LabelSelector : webhookSelector })
74
+ if err != nil {
75
+ return err
76
+ }
77
+
78
+ var webhook * admissionregistrationv1.MutatingWebhookConfiguration
79
+ for _ , item := range existingWebhooks .Items {
80
+ if strings .HasPrefix (item .Name , desc .Name ) {
81
+ webhook = & item
82
+ break
83
+ }
74
84
}
75
- existingHook , err := i . strategyClient . GetOpClient (). KubernetesInterface (). AdmissionregistrationV1 (). MutatingWebhookConfigurations (). Get ( context . TODO (), desc . Name , metav1. GetOptions {})
76
- if err = = nil {
85
+
86
+ if webhook ! = nil {
77
87
// Check if the only owners are this CSV or in this CSV's replacement chain
78
- if ownerutil .Adoptable ( i . owner , existingHook . GetOwnerReferences () ) {
79
- ownerutil .AddNonBlockingOwner ( existingHook , i .owner )
88
+ if ownerutil .AdoptableLabels ( webhook . GetLabels (), false , i . owner ) {
89
+ ownerutil .AddOwnerLabels ( webhook , i .owner )
80
90
}
81
91
82
92
// Update the list of webhooks
83
- existingHook .Webhooks = webhooks
93
+ webhook .Webhooks = []admissionregistrationv1.MutatingWebhook {
94
+ desc .GetMutatingWebhook (i .owner .GetNamespace (), ogNamespacelabelSelector , caPEM ),
95
+ }
84
96
85
97
// Attempt an update
86
- if _ , err := i .strategyClient .GetOpClient ().KubernetesInterface ().AdmissionregistrationV1 ().MutatingWebhookConfigurations ().Update (context .TODO (), existingHook , metav1.UpdateOptions {}); err != nil {
87
- log .Warnf ("could not update MutatingWebhookConfiguration %s" , existingHook .GetName ())
98
+ if _ , err := i .strategyClient .GetOpClient ().KubernetesInterface ().AdmissionregistrationV1 ().MutatingWebhookConfigurations ().Update (context .TODO (), webhook , metav1.UpdateOptions {}); err != nil {
99
+ log .Warnf ("could not update MutatingWebhookConfiguration %s" , webhook .GetName ())
88
100
return err
89
101
}
90
- } else if k8serrors .IsNotFound (err ) {
91
- hook := admissionregistrationv1.MutatingWebhookConfiguration {
92
- ObjectMeta : metav1.ObjectMeta {Name : desc .Name ,
93
- Namespace : i .owner .GetNamespace (),
102
+ } else {
103
+ // Create a ValidatingWebhookConfiguration
104
+ hook := admissionregistrationv1.ValidatingWebhookConfiguration {
105
+ ObjectMeta : metav1.ObjectMeta {
106
+ GenerateName : desc .Name + "-" ,
107
+ Namespace : i .owner .GetNamespace (),
108
+ Labels : ownerutil .OwnerLabel (i .owner , i .owner .GetObjectKind ().GroupVersionKind ().Kind ),
109
+ },
110
+ Webhooks : []admissionregistrationv1.ValidatingWebhook {
111
+ desc .GetValidatingWebhook (i .owner .GetNamespace (), ogNamespacelabelSelector , caPEM ),
94
112
},
95
- Webhooks : webhooks ,
96
113
}
97
- // Add an owner
98
- ownerutil .AddNonBlockingOwner (& hook , i .owner )
99
- if _ , err := i .strategyClient .GetOpClient ().KubernetesInterface ().AdmissionregistrationV1 ().MutatingWebhookConfigurations ().Create (context .TODO (), & hook , metav1.CreateOptions {}); err != nil {
100
- log .Errorf ("Webhooks: Error creating mutating MutatingVebhookConfiguration: %v" , err )
114
+
115
+ if _ , err := i .strategyClient .GetOpClient ().KubernetesInterface ().AdmissionregistrationV1 ().ValidatingWebhookConfigurations ().Create (context .TODO (), & hook , metav1.CreateOptions {}); err != nil {
116
+ log .Errorf ("Webhooks: Error creating ValidationWebhookConfiguration: %v" , err )
101
117
return err
102
118
}
103
- } else {
104
- return err
105
119
}
106
-
107
120
return nil
108
121
}
109
122
110
123
func (i * StrategyDeploymentInstaller ) createOrUpdateValidatingWebhook (ogNamespacelabelSelector * metav1.LabelSelector , caPEM []byte , desc v1alpha1.WebhookDescription ) error {
111
- webhooks := []admissionregistrationv1.ValidatingWebhook {
112
- desc .GetValidatingWebhook (i .owner .GetNamespace (), ogNamespacelabelSelector , caPEM ),
124
+ webhookSelector := labels .SelectorFromSet (ownerutil .OwnerLabel (i .owner , i .owner .GetObjectKind ().GroupVersionKind ().Kind )).String ()
125
+ existingWebhooks , err := i .strategyClient .GetOpClient ().KubernetesInterface ().AdmissionregistrationV1 ().ValidatingWebhookConfigurations ().List (context .TODO (), metav1.ListOptions {LabelSelector : webhookSelector })
126
+ if err != nil {
127
+ return err
128
+ }
129
+
130
+ var webhook * admissionregistrationv1.ValidatingWebhookConfiguration
131
+ for _ , item := range existingWebhooks .Items {
132
+ if strings .HasPrefix (item .Name , desc .Name ) {
133
+ webhook = & item
134
+ break
135
+ }
113
136
}
114
- existingHook , err := i . strategyClient . GetOpClient (). KubernetesInterface (). AdmissionregistrationV1 (). ValidatingWebhookConfigurations (). Get ( context . TODO (), desc . Name , metav1. GetOptions {})
115
- if err = = nil {
137
+
138
+ if webhook ! = nil {
116
139
// Check if the only owners are this CSV or in this CSV's replacement chain
117
- if ownerutil .Adoptable ( i . owner , existingHook . GetOwnerReferences () ) {
118
- ownerutil .AddNonBlockingOwner ( existingHook , i .owner )
140
+ if ownerutil .AdoptableLabels ( webhook . GetLabels (), false , i . owner ) {
141
+ ownerutil .AddOwnerLabels ( webhook , i .owner )
119
142
}
120
143
121
144
// Update the list of webhooks
122
- existingHook .Webhooks = webhooks
145
+ webhook .Webhooks = []admissionregistrationv1.ValidatingWebhook {
146
+ desc .GetValidatingWebhook (i .owner .GetNamespace (), ogNamespacelabelSelector , caPEM ),
147
+ }
123
148
124
149
// Attempt an update
125
- if _ , err := i .strategyClient .GetOpClient ().KubernetesInterface ().AdmissionregistrationV1 ().ValidatingWebhookConfigurations ().Update (context .TODO (), existingHook , metav1.UpdateOptions {}); err != nil {
126
- log .Warnf ("could not update ValidatingWebhookConfiguration %s" , existingHook .GetName ())
150
+ if _ , err := i .strategyClient .GetOpClient ().KubernetesInterface ().AdmissionregistrationV1 ().ValidatingWebhookConfigurations ().Update (context .TODO (), webhook , metav1.UpdateOptions {}); err != nil {
151
+ log .Warnf ("could not update ValidatingWebhookConfiguration %s" , webhook .GetName ())
127
152
return err
128
153
}
129
- } else if k8serrors . IsNotFound ( err ) {
154
+ } else {
130
155
// Create a ValidatingWebhookConfiguration
131
156
hook := admissionregistrationv1.ValidatingWebhookConfiguration {
132
- ObjectMeta : metav1.ObjectMeta {Name : desc .Name ,
133
- Namespace : i .owner .GetNamespace (),
157
+ ObjectMeta : metav1.ObjectMeta {
158
+ GenerateName : desc .Name + "-" ,
159
+ Namespace : i .owner .GetNamespace (),
160
+ Labels : ownerutil .OwnerLabel (i .owner , i .owner .GetObjectKind ().GroupVersionKind ().Kind ),
161
+ },
162
+ Webhooks : []admissionregistrationv1.ValidatingWebhook {
163
+ desc .GetValidatingWebhook (i .owner .GetNamespace (), ogNamespacelabelSelector , caPEM ),
134
164
},
135
- Webhooks : webhooks ,
136
165
}
137
166
138
- // Add an owner
139
- ownerutil .AddNonBlockingOwner (& hook , i .owner )
140
167
if _ , err := i .strategyClient .GetOpClient ().KubernetesInterface ().AdmissionregistrationV1 ().ValidatingWebhookConfigurations ().Create (context .TODO (), & hook , metav1.CreateOptions {}); err != nil {
141
- log .Errorf ("Webhooks: Error create creating ValidationVebhookConfiguration : %v" , err )
168
+ log .Errorf ("Webhooks: Error creating ValidationWebhookConfiguration : %v" , err )
142
169
return err
143
170
}
144
- } else {
145
- return err
146
171
}
147
172
return nil
148
173
}
0 commit comments