Skip to content

Commit

Permalink
Refactor ReverseProxy code
Browse files Browse the repository at this point in the history
  • Loading branch information
AkshaySainiDell committed Dec 12, 2024
1 parent e15599d commit d895a6b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 58 deletions.
27 changes: 12 additions & 15 deletions controllers/csm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,9 +731,9 @@ func (r *ContainerStorageModuleReconciler) SyncCSM(ctx context.Context, cr csmv1
}

// Create/Update Reverseproxy Server
if reverseProxyEnabled, _ := utils.IsModuleEnabled(ctx, cr, csmv1.ReverseProxy); reverseProxyEnabled {
if reverseProxyEnabled, _ := utils.IsModuleEnabled(ctx, cr, csmv1.ReverseProxy); reverseProxyEnabled && !modules.IsReverseProxySidecar() {
log.Infow("Trying Create/Update reverseproxy...")
if err := r.reconcileReverseProxy(ctx, false, operatorConfig, cr, ctrlClient); err != nil {
if err := r.reconcileReverseProxyServer(ctx, false, operatorConfig, cr, ctrlClient); err != nil {
return fmt.Errorf("failed to deploy reverseproxy proxy server: %v", err)
}
}
Expand Down Expand Up @@ -1160,16 +1160,13 @@ func getDriverConfig(ctx context.Context,
}, nil
}

// reconcileReverseProxy - deploy reverse proxy server
func (r *ContainerStorageModuleReconciler) reconcileReverseProxy(ctx context.Context, isDeleting bool, op utils.OperatorConfig, cr csmv1.ContainerStorageModule, ctrlClient client.Client) error {
// reconcileReverseProxyServer - deploy reverse proxy server
func (r *ContainerStorageModuleReconciler) reconcileReverseProxyServer(ctx context.Context, isDeleting bool, op utils.OperatorConfig, cr csmv1.ContainerStorageModule, ctrlClient client.Client) error {
log := logger.GetLogger(ctx)
log.Infow("Reconcile reverseproxy proxy")
if err := modules.ReverseProxyServer(ctx, isDeleting, op, cr, ctrlClient); err != nil {
return fmt.Errorf("unable to reconcile reverse-proxy server: %v", err)
}
if err := modules.ReverseProxyStartService(ctx, isDeleting, op, cr, ctrlClient); err != nil {
return fmt.Errorf("unable to reconcile reverse-proxy service: %v", err)
}
return nil
}

Expand Down Expand Up @@ -1272,12 +1269,6 @@ 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
}
Expand All @@ -1300,6 +1291,12 @@ func (r *ContainerStorageModuleReconciler) removeDriver(ctx context.Context, ins
}
}

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)
}
}
}

return nil
Expand All @@ -1322,9 +1319,9 @@ func (r *ContainerStorageModuleReconciler) removeModule(ctx context.Context, ins
return err
}
}
if reverseproxyEnabled, _ := utils.IsModuleEnabled(ctx, instance, csmv1.ReverseProxy); reverseproxyEnabled {
if reverseproxyEnabled, _ := utils.IsModuleEnabled(ctx, instance, csmv1.ReverseProxy); reverseproxyEnabled && !modules.IsReverseProxySidecar() {
log.Infow("Deleting ReverseProxy")
if err := r.reconcileReverseProxy(ctx, true, operatorConfig, instance, ctrlClient); err != nil {
if err := r.reconcileReverseProxyServer(ctx, true, operatorConfig, instance, ctrlClient); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions controllers/csm_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func (suite *CSMControllerTestSuite) TestReconcileReverseProxyError() {
csm := shared.MakeCSM(csmName, suite.namespace, shared.PmaxConfigVersion)
csm.Spec.Modules = getReverseProxyModule()
reconciler := suite.createReconciler()
err := reconciler.reconcileReverseProxy(ctx, false, badOperatorConfig, csm, suite.fakeClient)
err := reconciler.reconcileReverseProxyServer(ctx, false, badOperatorConfig, csm, suite.fakeClient)
assert.NotNil(suite.T(), err)
}

Expand All @@ -300,7 +300,7 @@ func (suite *CSMControllerTestSuite) TestReconcileReverseProxyServiceError() {
reconciler := suite.createReconciler()
_ = modules.ReverseProxyPrecheck(ctx, operatorConfig, revProxy[0], csm, reconciler)
revProxy[0].ConfigVersion = ""
err := reconciler.reconcileReverseProxy(ctx, false, badOperatorConfig, csm, suite.fakeClient)
err := reconciler.reconcileReverseProxyServer(ctx, false, badOperatorConfig, csm, suite.fakeClient)
assert.NotNil(suite.T(), err)
}

Expand Down
48 changes: 7 additions & 41 deletions pkg/modules/reverseproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,7 @@ func ReverseProxyPrecheck(ctx context.Context, op utils.OperatorConfig, revproxy
// ReverseProxyServer - apply/delete deployment objects
func ReverseProxyServer(ctx context.Context, isDeleting bool, op utils.OperatorConfig, cr csmv1.ContainerStorageModule, ctrlClient crclient.Client) error {
log := logger.GetLogger(ctx)
log.Infof("Checking if DeployAsSidecar is false...\n")
if deployAsSidecar {
log.Infof("DeployAsSidecar is true...csi-reverseproxy will be installed as sidecar\n")
log.Infof("exiting ReverseProxyServer...\n")
return nil
}
YamlString, err := getReverseProxyDeployment(op, cr, csmv1.Module{})
YamlString, err := getReverseProxyDeployment(op, cr)
if err != nil {
return err
}
Expand Down Expand Up @@ -222,7 +216,7 @@ func getReverseProxyService(op utils.OperatorConfig, cr csmv1.ContainerStorageMo
}

// getReverseProxyDeployment - updates deployment manifest with reverseproxy CRD values
func getReverseProxyDeployment(op utils.OperatorConfig, cr csmv1.ContainerStorageModule, revProxy csmv1.Module) (string, error) {
func getReverseProxyDeployment(op utils.OperatorConfig, cr csmv1.ContainerStorageModule) (string, error) {
YamlString := ""
revProxy, err := getReverseProxyModule(cr)
if err != nil {
Expand All @@ -237,42 +231,16 @@ func getReverseProxyDeployment(op utils.OperatorConfig, cr csmv1.ContainerStorag

YamlString = string(buf)
proxyNamespace := cr.Namespace
var proxyTLSSecret, proxyPort, proxyConfig string

// we will populate default values for environment variables, if nothing is given (minimal yaml)
if revProxy.Components == nil {
components := make([]csmv1.ContainerTemplate, 0)
components = append(components, csmv1.ContainerTemplate{
Name: "csipowermax-reverseproxy",
})
revProxy.Components = components

revProxy.Components[0].Envs = append(revProxy.Components[0].Envs, corev1.EnvVar{
Name: "X_CSI_REVPROXY_TLS_SECRET",
Value: "csirevproxy-tls-secret",
})
revProxy.Components[0].Envs = append(revProxy.Components[0].Envs, corev1.EnvVar{
Name: "X_CSI_REVPROXY_PORT",
Value: "2222",
})
revProxy.Components[0].Envs = append(revProxy.Components[0].Envs, corev1.EnvVar{
Name: "X_CSI_CONFIG_MAP_NAME",
Value: "powermax-reverseproxy-config",
})
revProxy.Components[0].Envs = append(revProxy.Components[0].Envs, corev1.EnvVar{
Name: "DeployAsSidecar",
Value: "true",
})

}
proxyTLSSecret := RevProxyTLSSecretDefaultName
proxyPort := RevProxyDefaultPort
proxyConfig := RevProxyConfigMapDeafultName
image := op.K8sVersion.Images.CSIRevProxy

for _, component := range revProxy.Components {
if component.Name == ReverseProxyServerComponent {
image := op.K8sVersion.Images.CSIRevProxy
if string(component.Image) != "" {
image = string(component.Image)
}
YamlString = strings.ReplaceAll(YamlString, ReverseProxyImage, image)
for _, env := range component.Envs {
if env.Name == "X_CSI_REVPROXY_TLS_SECRET" {
proxyTLSSecret = env.Value
Expand All @@ -283,13 +251,11 @@ func getReverseProxyDeployment(op utils.OperatorConfig, cr csmv1.ContainerStorag
if env.Name == "X_CSI_CONFIG_MAP_NAME" {
proxyConfig = env.Value
}
if env.Name == "DeployAsSidecar" {
deployAsSidecar, _ = strconv.ParseBool(env.Value)
}
}
}
}

YamlString = strings.ReplaceAll(YamlString, ReverseProxyImage, image)
YamlString = strings.ReplaceAll(YamlString, utils.DefaultReleaseNamespace, proxyNamespace)
YamlString = strings.ReplaceAll(YamlString, ReverseProxyPort, proxyPort)
YamlString = strings.ReplaceAll(YamlString, ReverseProxyTLSSecret, proxyTLSSecret)
Expand Down

0 comments on commit d895a6b

Please sign in to comment.