5
5
6
6
log "github.com/sirupsen/logrus"
7
7
appsv1 "k8s.io/api/apps/v1"
8
- corev1 "k8s.io/api/core/v1"
9
8
rbac "k8s.io/api/rbac/v1"
10
- apierrors "k8s.io/apimachinery/pkg/api/errors"
11
9
12
10
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/wrappers"
13
11
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
@@ -38,9 +36,10 @@ type StrategyDetailsDeployment struct {
38
36
}
39
37
40
38
type StrategyDeploymentInstaller struct {
41
- strategyClient wrappers.InstallStrategyDeploymentInterface
42
- owner ownerutil.Owner
43
- previousStrategy Strategy
39
+ strategyClient wrappers.InstallStrategyDeploymentInterface
40
+ owner ownerutil.Owner
41
+ previousStrategy Strategy
42
+ templateAnnotations map [string ]string
44
43
}
45
44
46
45
func (d * StrategyDetailsDeployment ) GetStrategyName () string {
@@ -50,70 +49,27 @@ func (d *StrategyDetailsDeployment) GetStrategyName() string {
50
49
var _ Strategy = & StrategyDetailsDeployment {}
51
50
var _ StrategyInstaller = & StrategyDeploymentInstaller {}
52
51
53
- func NewStrategyDeploymentInstaller (strategyClient wrappers.InstallStrategyDeploymentInterface , owner ownerutil.Owner , previousStrategy Strategy ) StrategyInstaller {
52
+ func NewStrategyDeploymentInstaller (strategyClient wrappers.InstallStrategyDeploymentInterface , templateAnnotations map [ string ] string , owner ownerutil.Owner , previousStrategy Strategy ) StrategyInstaller {
54
53
return & StrategyDeploymentInstaller {
55
- strategyClient : strategyClient ,
56
- owner : owner ,
57
- previousStrategy : previousStrategy ,
54
+ strategyClient : strategyClient ,
55
+ owner : owner ,
56
+ previousStrategy : previousStrategy ,
57
+ templateAnnotations : templateAnnotations ,
58
58
}
59
59
}
60
60
61
- func (i * StrategyDeploymentInstaller ) installPermissions (perms []StrategyDeploymentPermissions ) error {
62
- for _ , permission := range perms {
63
- // create role
64
- role := & rbac.Role {
65
- Rules : permission .Rules ,
66
- }
67
- ownerutil .AddNonBlockingOwner (role , i .owner )
68
- role .SetGenerateName (fmt .Sprintf ("%s-role-" , i .owner .GetName ()))
69
- createdRole , err := i .strategyClient .CreateRole (role )
70
- if err != nil {
71
- return err
72
- }
73
-
74
- // create serviceaccount if necessary
75
- serviceAccount := & corev1.ServiceAccount {}
76
- serviceAccount .SetName (permission .ServiceAccountName )
77
- // EnsureServiceAccount verifies/creates ownerreferences so we don't add them here
78
- serviceAccount , err = i .strategyClient .EnsureServiceAccount (serviceAccount , i .owner )
79
- if err != nil {
80
- return err
81
- }
82
-
83
- // create rolebinding
84
- roleBinding := & rbac.RoleBinding {
85
- RoleRef : rbac.RoleRef {
86
- Kind : "Role" ,
87
- Name : createdRole .GetName (),
88
- APIGroup : rbac .GroupName },
89
- Subjects : []rbac.Subject {{
90
- Kind : "ServiceAccount" ,
91
- Name : permission .ServiceAccountName ,
92
- Namespace : i .owner .GetNamespace (),
93
- }},
94
- }
95
- ownerutil .AddNonBlockingOwner (roleBinding , i .owner )
96
- roleBinding .SetGenerateName (fmt .Sprintf ("%s-%s-rolebinding-" , createdRole .Name , serviceAccount .Name ))
97
-
98
- if _ , err := i .strategyClient .CreateRoleBinding (roleBinding ); err != nil {
99
- return err
100
- }
101
- }
102
- return nil
103
- }
104
-
105
61
func (i * StrategyDeploymentInstaller ) installDeployments (deps []StrategyDeploymentSpec ) error {
106
62
for _ , d := range deps {
107
- // Create or Update Deployment
108
63
dep := & appsv1.Deployment {Spec : d .Spec }
109
64
dep .SetName (d .Name )
110
65
dep .SetNamespace (i .owner .GetNamespace ())
66
+ dep .Spec .Template .SetAnnotations (i .templateAnnotations )
111
67
ownerutil .AddNonBlockingOwner (dep , i .owner )
112
68
if dep .Labels == nil {
113
69
dep .SetLabels (map [string ]string {})
114
70
}
115
- dep .Labels ["alm- owner-name " ] = i .owner .GetName ()
116
- dep .Labels ["alm- owner- namespace" ] = i .owner .GetNamespace ()
71
+ dep .Labels ["olm. owner" ] = i .owner .GetName ()
72
+ dep .Labels ["olm. owner. namespace" ] = i .owner .GetNamespace ()
117
73
if _ , err := i .strategyClient .CreateOrUpdateDeployment (dep ); err != nil {
118
74
return err
119
75
}
@@ -167,33 +123,13 @@ func (i *StrategyDeploymentInstaller) CheckInstalled(s Strategy) (installed bool
167
123
return false , StrategyError {Reason : StrategyErrReasonInvalidStrategy , Message : fmt .Sprintf ("attempted to check %s strategy with deployment installer" , strategy .GetStrategyName ())}
168
124
}
169
125
170
- // Check service accounts
171
- for _ , perm := range strategy .Permissions {
172
- if err := i .checkForServiceAccount (perm .ServiceAccountName ); err != nil {
173
- return false , err
174
- }
175
- }
176
-
177
126
// Check deployments
178
127
if err := i .checkForDeployments (strategy .DeploymentSpecs ); err != nil {
179
128
return false , err
180
129
}
181
130
return true , nil
182
131
}
183
132
184
- func (i * StrategyDeploymentInstaller ) checkForServiceAccount (serviceAccountName string ) error {
185
- if _ , err := i .strategyClient .GetServiceAccountByName (serviceAccountName ); err != nil {
186
- if apierrors .IsNotFound (err ) {
187
- log .Debugf ("service account not found: %s" , serviceAccountName )
188
- return StrategyError {Reason : StrategyErrReasonComponentMissing , Message : fmt .Sprintf ("service account not found: %s" , serviceAccountName )}
189
- }
190
- log .Debugf ("error querying for %s: %s" , serviceAccountName , err )
191
- return StrategyError {Reason : StrategyErrReasonComponentMissing , Message : fmt .Sprintf ("error querying for %s: %s" , serviceAccountName , err )}
192
- }
193
- // TODO: use a SelfSubjectRulesReview (or a sync version) to verify ServiceAccount has correct access
194
- return nil
195
- }
196
-
197
133
func (i * StrategyDeploymentInstaller ) checkForDeployments (deploymentSpecs []StrategyDeploymentSpec ) error {
198
134
var depNames []string
199
135
for _ , dep := range deploymentSpecs {
@@ -224,6 +160,16 @@ func (i *StrategyDeploymentInstaller) checkForDeployments(deploymentSpecs []Stra
224
160
if ! ready {
225
161
return StrategyError {Reason : StrategyErrReasonWaiting , Message : fmt .Sprintf ("waiting for deployment %s to become ready: %s" , dep .Name , reason )}
226
162
}
163
+
164
+ // check annotations
165
+ if len (i .templateAnnotations ) > 0 && dep .Spec .Template .Annotations == nil {
166
+ return StrategyError {Reason : StrategyErrReasonAnnotationsMissing , Message : fmt .Sprintf ("no annotations found on deployment" )}
167
+ }
168
+ for key , value := range i .templateAnnotations {
169
+ if dep .Spec .Template .Annotations [key ] != value {
170
+ return StrategyError {Reason : StrategyErrReasonAnnotationsMissing , Message : fmt .Sprintf ("annotations on deployment don't match. couldn't find %s: %s" , key , value )}
171
+ }
172
+ }
227
173
}
228
174
return nil
229
175
}
0 commit comments