From 9c4fa54238c22eb1209d7eb53df2761b078fa74f Mon Sep 17 00:00:00 2001 From: Akshay Saini Date: Wed, 11 Dec 2024 19:32:33 +0530 Subject: [PATCH] Inject ReverseProxy directly into the deployment --- controllers/csm_controller.go | 32 ++++++++++--- pkg/drivers/powermax.go | 10 ---- pkg/modules/reverseproxy.go | 88 ++++++++++++++--------------------- 3 files changed, 60 insertions(+), 70 deletions(-) diff --git a/controllers/csm_controller.go b/controllers/csm_controller.go index e18a4b50..f52753cd 100644 --- a/controllers/csm_controller.go +++ b/controllers/csm_controller.go @@ -759,6 +759,25 @@ func (r *ContainerStorageModuleReconciler) SyncCSM(ctx context.Context, cr csmv1 node := driverConfig.Node controller := driverConfig.Controller + if cr.GetDriverType() == csmv1.PowerMax { + if !modules.IsReverseProxySidecar() { + log.Infof("DeployAsSidar is false...csi-reverseproxy should be present as deployement\n") + log.Infof("adding proxy service name...\n") + modules.AddReverseProxyServiceName(&controller.Deployment) + } else { + log.Info("Starting CSI ReverseProxy Service") + if err := modules.ReverseProxyStartService(ctx, false, operatorConfig, cr, ctrlClient); err != nil { + return fmt.Errorf("unable to reconcile reverse-proxy service: %v", err) + } + log.Info("Injecting CSI ReverseProxy") + dp, err := modules.ReverseProxyInjectDeployment(controller.Deployment, cr, operatorConfig) + if err != nil { + return fmt.Errorf("injecting replication into deployment: %v", err) + } + controller.Deployment = *dp + } + } + replicationEnabled, clusterClients, err := utils.GetDefaultClusters(ctx, cr, r) if err != nil { return err @@ -827,13 +846,6 @@ func (r *ContainerStorageModuleReconciler) SyncCSM(ctx context.Context, cr csmv1 } controller.Rbac.ClusterRole = *clusterRole - case csmv1.ReverseProxy: - log.Info("Injecting CSI ReverseProxy") - dp, err := modules.ReverseProxyInjectDeployment(controller.Deployment, cr, operatorConfig) - if err != nil { - return fmt.Errorf("injecting replication into deployment: %v", err) - } - controller.Deployment = *dp } } } @@ -1260,6 +1272,12 @@ func (r *ContainerStorageModuleReconciler) removeDriver(ctx context.Context, ins return err } for _, cluster := range clusterClients { + if instance.GetDriverType() == csmv1.PowerMax && modules.IsReverseProxySidecar() { + log.Info("Removing CSI ReverseProxy Service") + if err := modules.ReverseProxyStartService(ctx, true, operatorConfig, instance, cluster.ClusterCTRLClient); err != nil { + return fmt.Errorf("unable to reconcile reverse-proxy service: %v", err) + } + } if err = removeDriverReplicaCluster(ctx, cluster, driverConfig); err != nil { return err } diff --git a/pkg/drivers/powermax.go b/pkg/drivers/powermax.go index 91dd5da7..105fe2c4 100644 --- a/pkg/drivers/powermax.go +++ b/pkg/drivers/powermax.go @@ -86,23 +86,13 @@ func PrecheckPowerMax(ctx context.Context, cr *csmv1.ContainerStorageModule, ope } } - foundRevProxy := false for i, mod := range cr.Spec.Modules { if mod.Name == csmv1.ReverseProxy { - foundRevProxy = true cr.Spec.Modules[i].Enabled = true cr.Spec.Modules[i].ForceRemoveModule = true break } } - if !foundRevProxy { - // if we are here then it's minimal yaml - log.Infof("Reverse proxy module not found adding it") - cr.Spec.Modules = append(cr.Spec.Modules, csmv1.Module{ - Name: csmv1.ReverseProxy, - Enabled: true, - }) - } log.Debugw("preCheck", "secrets", cred) return nil diff --git a/pkg/modules/reverseproxy.go b/pkg/modules/reverseproxy.go index 23bcbed9..90dbd7b8 100644 --- a/pkg/modules/reverseproxy.go +++ b/pkg/modules/reverseproxy.go @@ -49,13 +49,15 @@ const ( // var used in deploying reverseproxy var ( - deployAsSidecar = true - CSIPmaxRevProxyServiceName = "X_CSI_POWERMAX_PROXY_SERVICE_NAME" - CSIPmaxRevProxyPort = "X_CSI_POWERMAX_SIDECAR_PROXY_PORT" - RevProxyDefaultPort = "2222" - RevProxyServiceName = "csipowermax-reverseproxy" - RevProxyConfigMapVolName = "configmap-volume" - RevProxyTLSSecretVolName = "tls-secret" + deployAsSidecar = true + CSIPmaxRevProxyServiceName = "X_CSI_POWERMAX_PROXY_SERVICE_NAME" + CSIPmaxRevProxyPort = "X_CSI_POWERMAX_SIDECAR_PROXY_PORT" + RevProxyDefaultPort = "2222" + RevProxyServiceName = "csipowermax-reverseproxy" + RevProxyConfigMapVolName = "configmap-volume" + RevProxyConfigMapDeafultName = "powermax-reverseproxy-config" + RevProxyTLSSecretVolName = "tls-secret" + RevProxyTLSSecretDefaultName = "csirevproxy-tls-secret" ) // ReverseproxySupportedDrivers is a map containing the CSI Drivers supported by CSM Reverseproxy. The key is driver name and the value is the driver plugin identifier @@ -152,10 +154,7 @@ func ReverseProxyServer(ctx context.Context, isDeleting bool, op utils.OperatorC // ReverseProxyStartService starts reverseproxy service for node to connect to revserseproxy sidecar func ReverseProxyStartService(ctx context.Context, isDeleting bool, op utils.OperatorConfig, cr csmv1.ContainerStorageModule, ctrlClient crclient.Client) error { log := logger.GetLogger(ctx) - if !deployAsSidecar { - log.Infof("DeployAsSidar is false...csi-reverseproxy service is part of deployement already\n") - return nil - } + YamlString, err := getReverseProxyService(op, cr) if err != nil { return err @@ -193,9 +192,10 @@ func getReverseProxyModule(cr csmv1.ContainerStorageModule) (csmv1.Module, error // getReverseProxyService - gets the reverseproxy service manifest func getReverseProxyService(op utils.OperatorConfig, cr csmv1.ContainerStorageModule) (string, error) { yamlString := "" - revProxy, err := getReverseProxyModule(cr) - if err != nil { - return yamlString, err + revProxy := cr.GetModule(csmv1.ReverseProxy) + // This is necessary for the minimal manifest, where the reverse proxy will not be included in the CSM CR. + if len(revProxy.Name) == 0 { + revProxy.Name = csmv1.ReverseProxy } buf, err := readConfigFile(revProxy, cr, op, ReverseProxyService) @@ -300,45 +300,11 @@ func getReverseProxyDeployment(op utils.OperatorConfig, cr csmv1.ContainerStorag // ReverseProxyInjectDeployment injects reverseproxy container as sidecar into controller func ReverseProxyInjectDeployment(dp v1.DeploymentApplyConfiguration, cr csmv1.ContainerStorageModule, op utils.OperatorConfig) (*v1.DeploymentApplyConfiguration, error) { - log := logger.GetLogger(context.Background()) - - if !deployAsSidecar { - log.Infof("DeployAsSidar is false...csi-reverseproxy should be present as deployement\n") - log.Infof("adding proxy service name...\n") - for i, cnt := range dp.Spec.Template.Spec.Containers { - if *cnt.Name == "driver" { - dp.Spec.Template.Spec.Containers[i].Env = append(dp.Spec.Template.Spec.Containers[i].Env, - acorev1.EnvVarApplyConfiguration{Name: &CSIPmaxRevProxyServiceName, Value: &RevProxyServiceName}, - ) - break - } - } - return &dp, nil - } - revProxyModule, containerPtr, err := getRevproxyApplyCR(cr, op) if err != nil { return nil, err } - if revProxyModule.Components == nil { - revProxyModule = &csmv1.Module{ - Name: csmv1.ReverseProxy, - ForceRemoveModule: true, - Enabled: true, - Components: []csmv1.ContainerTemplate{ - { - Image: csmv1.ImageType(op.K8sVersion.Images.CSIRevProxy), - Name: "csipowermax-reverseproxy", - Envs: []corev1.EnvVar{ - {Name: "X_CSI_REVPROXY_TLS_SECRET", Value: "csirevproxy-tls-secret"}, - {Name: "X_CSI_REVPROXY_PORT", Value: "2222"}, - {Name: "X_CSI_CONFIG_MAP_NAME", Value: "powermax-reverseproxy-config"}, - {Name: "DeployAsSidecar", Value: "true"}, - }, - }, - }, - } - } + container := *containerPtr // update the image for _, side := range revProxyModule.Components { @@ -382,7 +348,7 @@ func getRevProxyPort(revProxyModule csmv1.Module) string { } func getRevProxyVolumeComp(revProxyModule csmv1.Module) []acorev1.VolumeApplyConfiguration { - var revProxyConfigMap, revProxyTLSSecret string + var revProxyConfigMap, revProxyTLSSecret = RevProxyConfigMapDeafultName, RevProxyTLSSecretDefaultName for _, component := range revProxyModule.Components { if component.Name == ReverseProxyServerComponent { for _, env := range component.Envs { @@ -422,9 +388,10 @@ func getRevProxyVolumeComp(revProxyModule csmv1.Module) []acorev1.VolumeApplyCon // returns revproxy module and container func getRevproxyApplyCR(cr csmv1.ContainerStorageModule, op utils.OperatorConfig) (*csmv1.Module, *acorev1.ContainerApplyConfiguration, error) { var err error - revProxyModule, err := getReverseProxyModule(cr) - if err != nil { - return nil, nil, err + revProxyModule := cr.GetModule(csmv1.ReverseProxy) + // This is necessary for the minimal manifest, where the reverse proxy will not be included in the CSM CR. + if len(revProxyModule.Name) == 0 { + revProxyModule.Name = csmv1.ReverseProxy } buf, err := readConfigFile(revProxyModule, cr, op, ReverseProxySidecar) @@ -440,3 +407,18 @@ func getRevproxyApplyCR(cr csmv1.ContainerStorageModule, op utils.OperatorConfig } return &revProxyModule, &container, nil } + +func AddReverseProxyServiceName(dp *v1.DeploymentApplyConfiguration) { + for i, cnt := range dp.Spec.Template.Spec.Containers { + if *cnt.Name == "driver" { + dp.Spec.Template.Spec.Containers[i].Env = append(dp.Spec.Template.Spec.Containers[i].Env, + acorev1.EnvVarApplyConfiguration{Name: &CSIPmaxRevProxyServiceName, Value: &RevProxyServiceName}, + ) + break + } + } +} + +func IsReverseProxySidecar() bool { + return deployAsSidecar +}