Skip to content

Commit

Permalink
fix: higress-controller recreate many times
Browse files Browse the repository at this point in the history
Signed-off-by: charlie <qianglin98@qq.com>
  • Loading branch information
Charlie17Li committed Jul 30, 2023
1 parent 6c2e4b3 commit c6c8c07
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 73 deletions.
67 changes: 41 additions & 26 deletions internal/controller/higresscontroller/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,36 @@ func initDeployment(deploy *appsv1.Deployment, instance *operatorv1alpha1.Higres
}

func updateDeploymentSpec(deploy *appsv1.Deployment, instance *operatorv1alpha1.HigressController) {
deploy.Spec.Selector = &metav1.LabelSelector{
MatchLabels: instance.Spec.SelectorLabels,
}
deploy.Spec.Selector = &metav1.LabelSelector{MatchLabels: instance.Spec.SelectorLabels}

deploy.Spec.Replicas = instance.Spec.Replicas
deploy.Spec.Template = apiv1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: instance.Name,
Namespace: instance.Namespace,
Labels: instance.Spec.SelectorLabels,
},
Spec: apiv1.PodSpec{
ServiceAccountName: getServiceAccount(instance),
Containers: []apiv1.Container{
{
Name: genControllerName(instance),
Image: genImage(instance.Spec.Controller.Image.Repository, instance.Spec.Controller.Image.Tag),
ImagePullPolicy: instance.Spec.Controller.Image.ImagePullPolicy,
Args: genControllerArgs(instance),
Ports: genControllerPorts(instance),
SecurityContext: genControllerSecurityContext(instance),
Env: genControllerEnv(instance),
VolumeMounts: genControllerVolumeMounts(instance),
},
},
Volumes: genVolumes(instance),
},

controller.UpdateObjectMeta(&deploy.Spec.Template.ObjectMeta, instance, instance.Spec.SelectorLabels)

deploy.Spec.Template.Spec.ServiceAccountName = getServiceAccount(instance)

exist := false
for _, c := range deploy.Spec.Template.Spec.Containers {
if c.Name == genControllerName(instance) {
exist = true
break
}
}
if !exist {
deploy.Spec.Template.Spec.Containers = append(deploy.Spec.Template.Spec.Containers, apiv1.Container{
Name: genControllerName(instance),
Image: genImage(instance.Spec.Controller.Image.Repository, instance.Spec.Controller.Image.Tag),
ImagePullPolicy: instance.Spec.Controller.Image.ImagePullPolicy,
Args: genControllerArgs(instance),
Ports: genControllerPorts(instance),
SecurityContext: genControllerSecurityContext(instance),
Env: genControllerEnv(instance),
VolumeMounts: genControllerVolumeMounts(instance),
})
}

deploy.Spec.Template.Spec.Volumes = genVolumes(instance)

if !instance.Spec.EnableHigressIstio {
pilot := apiv1.Container{
Name: genPilotName(instance),
Expand All @@ -73,7 +76,16 @@ func updateDeploymentSpec(deploy *appsv1.Deployment, instance *operatorv1alpha1.
VolumeMounts: genPilotVolumeMounts(instance),
}

deploy.Spec.Template.Spec.Containers = append(deploy.Spec.Template.Spec.Containers, pilot)
exist = false
for _, c := range deploy.Spec.Template.Spec.Containers {
if c.Name == genPilotName(instance) {
exist = true
break
}
}
if !exist {
deploy.Spec.Template.Spec.Containers = append(deploy.Spec.Template.Spec.Containers, pilot)
}
}
}

Expand Down Expand Up @@ -420,6 +432,7 @@ func genControllerVolumeMounts(instance *operatorv1alpha1.HigressController) []a

func genVolumes(instance *operatorv1alpha1.HigressController) []apiv1.Volume {
optional := true
defaultMode := int32(420)
volumes := []apiv1.Volume{
{
Name: "log",
Expand All @@ -441,6 +454,7 @@ func genVolumes(instance *operatorv1alpha1.HigressController) []apiv1.Volume {
Secret: &apiv1.SecretVolumeSource{
SecretName: "cacerts",
Optional: &optional,
DefaultMode: &defaultMode,
},
},
},
Expand All @@ -450,6 +464,7 @@ func genVolumes(instance *operatorv1alpha1.HigressController) []apiv1.Volume {
Secret: &apiv1.SecretVolumeSource{
SecretName: "istio-kubeconfig",
Optional: &optional,
DefaultMode: &defaultMode,
},
},
},
Expand Down
87 changes: 56 additions & 31 deletions internal/controller/higresscontroller/rbac.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package higresscontroller

import (
"reflect"

rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand All @@ -9,6 +11,7 @@ import (
)

const (
role = "higress-controller"
clusterRole = "higress-controller"
)

Expand Down Expand Up @@ -152,7 +155,6 @@ func initClusterRole(cr *rbacv1.ClusterRole, instance *operatorv1alpha1.HigressC

func muteClusterRole(cr *rbacv1.ClusterRole, instance *operatorv1alpha1.HigressController) controllerutil.MutateFn {
return func() error {
cr.Name = clusterRole
cr.Rules = defaultRules()
return nil
}
Expand All @@ -167,26 +169,37 @@ func initClusterRoleBinding(crb *rbacv1.ClusterRoleBinding, instance *operatorv1
ObjectMeta: metav1.ObjectMeta{
Name: getServiceAccount(instance),
},
RoleRef: rbacv1.RoleRef{
Kind: "ClusterRole",
Name: clusterRole,
APIGroup: "rbac.authorization.k8s.io",
},
Subjects: []rbacv1.Subject{
{
Kind: "ServiceAccount",
Name: getServiceAccount(instance),
Namespace: instance.Namespace,
},
},
}

updateClusterRoleBinding(crb, instance)
return crb
}

func updateClusterRoleBinding(crb *rbacv1.ClusterRoleBinding, instance *operatorv1alpha1.HigressController) {
crb.RoleRef = rbacv1.RoleRef{
Kind: "ClusterRole",
Name: clusterRole,
APIGroup: "rbac.authorization.k8s.io",
}

subject := rbacv1.Subject{
Kind: "ServiceAccount",
Name: getServiceAccount(instance),
Namespace: instance.Namespace,
}

for _, sub := range crb.Subjects {
if reflect.DeepEqual(sub, subject) {
return
}
}

crb.Subjects = append(crb.Subjects, subject)
}

func muteClusterRoleBinding(crb *rbacv1.ClusterRoleBinding, instance *operatorv1alpha1.HigressController) controllerutil.MutateFn {
return func() error {
crb = initClusterRoleBinding(crb, instance)
updateClusterRoleBinding(crb, instance)
return nil
}
}
Expand All @@ -197,39 +210,51 @@ func initRoleBinding(rb *rbacv1.RoleBinding, instance *operatorv1alpha1.HigressC
Name: getServiceAccount(instance),
Namespace: instance.Namespace,
},
RoleRef: rbacv1.RoleRef{
Kind: "Role",
Name: getServiceAccount(instance),
APIGroup: "rbac.authorization.k8s.io",
},
Subjects: []rbacv1.Subject{
{
Kind: "ServiceAccount",
Name: getServiceAccount(instance),
Namespace: instance.Namespace,
},
},
}

updateRoleBinding(rb, instance)
return rb
}

func updateRoleBinding(rb *rbacv1.RoleBinding, instance *operatorv1alpha1.HigressController) {
rb.RoleRef = rbacv1.RoleRef{
Kind: "Role",
Name: role,
APIGroup: "rbac.authorization.k8s.io",
}

subject := rbacv1.Subject{
Kind: "ServiceAccount",
Name: getServiceAccount(instance),
Namespace: instance.Namespace,
}

for _, sub := range rb.Subjects {
if reflect.DeepEqual(sub, subject) {
return
}
}

rb.Subjects = append(rb.Subjects, subject)
}

func muteRoleBinding(rb *rbacv1.RoleBinding, instance *operatorv1alpha1.HigressController) controllerutil.MutateFn {
return func() error {
initRoleBinding(rb, instance)
updateRoleBinding(rb, instance)
return nil
}
}

func initRole(role *rbacv1.Role, instance *operatorv1alpha1.HigressController) *rbacv1.Role {
*role = rbacv1.Role{
func initRole(r *rbacv1.Role, instance *operatorv1alpha1.HigressController) *rbacv1.Role {
*r = rbacv1.Role{
ObjectMeta: metav1.ObjectMeta{
Name: getServiceAccount(instance),
Name: role,
Namespace: instance.Namespace,
},
Rules: defaultRules(),
}

return role
return r
}

func muteRole(role *rbacv1.Role, instance *operatorv1alpha1.HigressController) controllerutil.MutateFn {
Expand Down
12 changes: 10 additions & 2 deletions internal/controller/higresscontroller/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,21 @@ func updateServiceSpec(svc *apiv1.Service, instance *operatorv1alpha1.HigressCon
Port: 15014,
},
}
svc.Spec.Ports = append(svc.Spec.Ports, ports...)
set := make(map[string]struct{})
for _, port := range svc.Spec.Ports {
set[port.Name] = struct{}{}
}
for _, port := range ports {
if _, ok := set[port.Name]; !ok {
svc.Spec.Ports = append(svc.Spec.Ports, port)
}
}
}
}

func muteService(svc *apiv1.Service, instance *operatorv1alpha1.HigressController) controllerutil.MutateFn {
return func() error {
initService(svc, instance)
updateServiceSpec(svc, instance)
return nil
}
}
63 changes: 49 additions & 14 deletions internal/controller/higressgateway/rbac.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package higressgateway

import (
"reflect"

rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -47,26 +49,37 @@ func initClusterRoleBinding(crb *rbacv1.ClusterRoleBinding, instance *operatorv1
ObjectMeta: metav1.ObjectMeta{
Name: getServiceAccount(instance),
},
RoleRef: rbacv1.RoleRef{
Kind: "ClusterRole",
Name: clusterRole,
APIGroup: "rbac.authorization.k8s.io",
},
Subjects: []rbacv1.Subject{
{
Kind: "ServiceAccount",
Name: getServiceAccount(instance),
Namespace: instance.Namespace,
},
},
}

updateClusterRoleBinding(crb, instance)
return crb
}

func updateClusterRoleBinding(crb *rbacv1.ClusterRoleBinding, instance *operatorv1alpha1.HigressGateway) {
crb.RoleRef = rbacv1.RoleRef{
Kind: "ClusterRole",
Name: clusterRole,
APIGroup: "rbac.authorization.k8s.io",
}

subject := rbacv1.Subject{
Kind: "ServiceAccount",
Name: getServiceAccount(instance),
Namespace: instance.Namespace,
}

for _, sub := range crb.Subjects {
if reflect.DeepEqual(sub, subject) {
return
}
}

crb.Subjects = append(crb.Subjects, subject)
}

func muteClusterRoleBinding(crb *rbacv1.ClusterRoleBinding, instance *operatorv1alpha1.HigressGateway) controllerutil.MutateFn {
return func() error {
initClusterRoleBinding(crb, instance)
updateClusterRoleBinding(crb, instance)
return nil
}
}
Expand All @@ -93,9 +106,31 @@ func initRoleBinding(rb *rbacv1.RoleBinding, instance *operatorv1alpha1.HigressG
return rb
}

func updateRoleBinding(rb *rbacv1.RoleBinding, instance *operatorv1alpha1.HigressGateway) {
rb.RoleRef = rbacv1.RoleRef{
Kind: "Role",
Name: role,
APIGroup: "rbac.authorization.k8s.io",
}

subject := rbacv1.Subject{
Kind: "ServiceAccount",
Name: getServiceAccount(instance),
Namespace: instance.Namespace,
}

for _, sub := range rb.Subjects {
if reflect.DeepEqual(sub, subject) {
return
}
}

rb.Subjects = append(rb.Subjects, subject)
}

func muteRoleBinding(rb *rbacv1.RoleBinding, instance *operatorv1alpha1.HigressGateway) controllerutil.MutateFn {
return func() error {
initRoleBinding(rb, instance)
updateRoleBinding(rb, instance)
return nil
}
}
Expand Down
Loading

0 comments on commit c6c8c07

Please sign in to comment.