Skip to content

Commit e620686

Browse files
Merge pull request #13 from IBM/fix/ie-for-ie
fixes for rolling update for ie-server to enable ie-for-ie scenario
2 parents 1ce5644 + 7f7a894 commit e620686

File tree

7 files changed

+124
-14
lines changed

7 files changed

+124
-14
lines changed

enforcer/pkg/policy/policy.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,14 @@ func (self *Policy) Validate(reqc *common.ReqContext, enforcerNs, policyNs strin
9595
return false, fmt.Sprintf("Policy in invalid format; %s", errMsg)
9696
}
9797
ns := reqc.Namespace
98-
ieNs := enforcerNs
98+
9999
polNs := policyNs
100100
pType := self.PolicyType
101-
if ns != ieNs && ns != polNs {
102-
return false, fmt.Sprintf("Policy must be created in namespace \"%s\" or \"%s\", but requested in \"%s\"", ieNs, polNs, ns)
103-
}
104-
if (pType == DefaultPolicy || pType == IEPolicy || pType == SignerPolicy) && ns != ieNs {
105-
return false, fmt.Sprintf("%s must be created in namespace \"%s\", but requested in \"%s\"", pType, ieNs, ns)
106-
}
101+
107102
if pType == CustomPolicy && ns != polNs {
108103
return false, fmt.Sprintf("%s must be created in namespace \"%s\", but requested in \"%s\"", pType, polNs, ns)
109104
}
110-
// op := self.Operation
111-
// if op == "UPDATE" && (pType == policy.DefaultPolicy || pType == policy.IEPolicy) {
112-
// return false, fmt.Sprintf("%s cannot be updated", pType)
113-
// }
105+
114106
return true, ""
115107
}
116108

operator/deploy/crds/research.ibm.com_v1alpha1_integrityenforcer_cr.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ spec:
6565
runAsUser: 1000
6666
stdOutput: true
6767
replicaCount: 1
68+
maxSurge: 1
69+
maxUnavailable: 0
6870
security:
6971
clusterRole: ie-cluster-role
7072
clusterRoleBinding: ie-cluster-role-binding
@@ -96,4 +98,4 @@ spec:
9698
policyNamespace: ie-policy
9799
webhookConfigName: ie-webhook-config
98100
webhookServerTlsSecretName: ie-server-tls
99-
webhookServiceName: ie-server
101+
webhookServiceName: ie-server

operator/pkg/apis/research/v1alpha1/integrityenforcer_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
policy "github.com/IBM/integrity-enforcer/enforcer/pkg/policy"
2222
v1 "k8s.io/api/core/v1"
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
intstr "k8s.io/apimachinery/pkg/util/intstr"
2425
)
2526

2627
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
@@ -31,6 +32,8 @@ type IntegrityEnforcerSpec struct {
3132
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
3233
// Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file
3334
// Add custom validation using kubebuilder tags: https://book-v1.book.kubebuilder.io/beyond_basics/generating_crd.html
35+
MaxSurge *intstr.IntOrString `json:"maxSurge,omitempty"`
36+
MaxUnavailable *intstr.IntOrString `json:"maxUnavailable,omitempty"`
3437
ReplicaCount *int32 `json:"replicaCount,omitempty"`
3538
MetaLabels map[string]string `json:"labels,omitempty"`
3639
SelectorLabels map[string]string `json:"selector,omitempty"`

operator/pkg/apis/research/v1alpha1/zz_generated.deepcopy.go

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

operator/pkg/controller/integrityenforcer/integrityenforcer.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,18 @@ func (r *ReconcileIntegrityEnforcer) createOrUpdateDeployment(instance *research
740740
return reconcile.Result{Requeue: true, RequeueAfter: time.Second * 1}, nil
741741
} else if err != nil {
742742
return reconcile.Result{}, err
743+
} else if !res.EqualDeployments(expected, found) {
744+
// If spec is incorrect, update it and requeue
745+
found.ObjectMeta.Labels = expected.ObjectMeta.Labels
746+
found.Spec = expected.Spec
747+
err = r.client.Update(context.TODO(), found)
748+
if err != nil {
749+
reqLogger.Error(err, "Failed to update Deployment", "Namespace", instance.Namespace, "Name", found.Name)
750+
return reconcile.Result{}, err
751+
}
752+
reqLogger.Info("Updating IntegrityEnforcer Controller Deployment", "Deployment.Name", found.Name)
753+
// Spec updated - return and requeue
754+
return reconcile.Result{Requeue: true}, nil
743755
}
744756

745757
// No extra validation

operator/pkg/resources/deploy.go

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package resources
1919
import (
2020
"fmt"
2121
"strconv"
22-
22+
"reflect"
2323
appsv1 "k8s.io/api/apps/v1"
2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2525

@@ -62,6 +62,17 @@ func BuildDeploymentForCR(cr *researchv1alpha1.IntegrityEnforcer) *appsv1.Deploy
6262
SecurityContext: cr.Spec.Server.SecurityContext,
6363
Image: cr.Spec.Server.Image,
6464
ImagePullPolicy: cr.Spec.Server.ImagePullPolicy,
65+
ReadinessProbe: &v1.Probe{
66+
InitialDelaySeconds: 30,
67+
PeriodSeconds: 30,
68+
Handler: v1.Handler{
69+
Exec: &v1.ExecAction{
70+
Command: []string{
71+
"ls",
72+
},
73+
},
74+
},
75+
},
6576
Ports: []v1.ContainerPort{
6677
{
6778
Name: "ac-api",
@@ -203,7 +214,12 @@ func BuildDeploymentForCR(cr *researchv1alpha1.IntegrityEnforcer) *appsv1.Deploy
203214
Labels: labels,
204215
},
205216
Spec: appsv1.DeploymentSpec{
206-
217+
Strategy: appsv1.DeploymentStrategy{
218+
RollingUpdate: &appsv1.RollingUpdateDeployment{
219+
MaxSurge: cr.Spec.MaxSurge,
220+
MaxUnavailable: cr.Spec.MaxUnavailable,
221+
},
222+
},
207223
Replicas: cr.Spec.ReplicaCount,
208224
Selector: &metav1.LabelSelector{
209225
MatchLabels: cr.Spec.SelectorLabels,
@@ -227,3 +243,76 @@ func BuildDeploymentForCR(cr *researchv1alpha1.IntegrityEnforcer) *appsv1.Deploy
227243
},
228244
}
229245
}
246+
247+
// EqualDeployments returns a Boolean
248+
func EqualDeployments(expected *appsv1.Deployment, found *appsv1.Deployment) bool {
249+
if !EqualLabels(found.ObjectMeta.Labels, expected.ObjectMeta.Labels) {
250+
return false
251+
}
252+
if !EqualPods(expected.Spec.Template, found.Spec.Template) {
253+
return false
254+
}
255+
return true
256+
}
257+
258+
// EqualPods returns a Boolean
259+
func EqualPods(expected v1.PodTemplateSpec, found v1.PodTemplateSpec) bool {
260+
if !EqualLabels(found.ObjectMeta.Labels, expected.ObjectMeta.Labels) {
261+
return false
262+
}
263+
if !EqualAnnotations(found.ObjectMeta.Annotations, expected.ObjectMeta.Annotations) {
264+
return false
265+
}
266+
if !reflect.DeepEqual(found.Spec.ServiceAccountName, expected.Spec.ServiceAccountName) {
267+
return false
268+
}
269+
if len(found.Spec.Containers) != len(expected.Spec.Containers) {
270+
return false
271+
}
272+
if !EqualContainers(expected.Spec.Containers[0], found.Spec.Containers[0]) {
273+
return false
274+
}
275+
return true
276+
}
277+
// EqualContainers returns a Boolean
278+
func EqualContainers(expected v1.Container, found v1.Container) bool {
279+
if !reflect.DeepEqual(found.Name, expected.Name) {
280+
return false
281+
}
282+
if !reflect.DeepEqual(found.Image, expected.Image) {
283+
return false
284+
}
285+
if !reflect.DeepEqual(found.ImagePullPolicy, expected.ImagePullPolicy) {
286+
return false
287+
}
288+
if !reflect.DeepEqual(found.VolumeMounts, expected.VolumeMounts) {
289+
return false
290+
}
291+
if !reflect.DeepEqual(found.SecurityContext, expected.SecurityContext) {
292+
return false
293+
}
294+
if !reflect.DeepEqual(found.Ports, expected.Ports) {
295+
return false
296+
}
297+
if !reflect.DeepEqual(found.Args, expected.Args) {
298+
return false
299+
}
300+
if !reflect.DeepEqual(found.Env, expected.Env) {
301+
return false
302+
}
303+
return true
304+
}
305+
306+
func EqualLabels(found map[string]string, expected map[string]string) bool {
307+
if !reflect.DeepEqual(found, expected) {
308+
return false
309+
}
310+
return true
311+
}
312+
313+
func EqualAnnotations(found map[string]string, expected map[string]string) bool {
314+
if !reflect.DeepEqual(found, expected) {
315+
return false
316+
}
317+
return true
318+
}

operator/resources/default-policy.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ spec:
1717
request: {}
1818
allowedForInternalRequest:
1919
- username: system:admin
20+
- username: system:serviceaccount:integrity-enforcer-ns:integrity-enforcer-operator
2021
- username: system:serviceaccount:openshift-marketplace:marketplace-operator
2122
- username: system:serviceaccount:openshift-monitoring:cluster-monitoring-operator
2223
- username: system:serviceaccount:openshift-network-operator:default

0 commit comments

Comments
 (0)