Skip to content

Commit

Permalink
Deployment apply (#13)
Browse files Browse the repository at this point in the history
Changes for deployment apply()
  • Loading branch information
abhi16394 authored Jan 27, 2022
1 parent 02ea493 commit 259e452
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 366 deletions.
91 changes: 87 additions & 4 deletions controllers/csm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,82 @@ func (r *ContainerStorageModuleReconciler) ignoreUpdatePredicate() predicate.Pre
}
}

func (r *ContainerStorageModuleReconciler) handleDeploymentUpdate(oldObj interface{}, obj interface{}) {

old, _ := oldObj.(*appsv1.Deployment)
d, _ := obj.(*appsv1.Deployment)
name := d.Spec.Template.Labels["csm"]
if name == "" {
r.Log.Info("deployment not found", "name", d.Name)
return
}

r.Log.Info("deployment modified generation", fmt.Sprintf("%d", d.Generation), fmt.Sprintf("%d", old.Generation))
desired := d.Status.UpdatedReplicas
available := d.Status.AvailableReplicas
ready := d.Status.ReadyReplicas

r.Log.Info("deployment csm", "desired", desired)
r.Log.Info("deployment csm", "numberReady", ready)
r.Log.Info("deployment csm", "available", available)

ns := d.Namespace
r.Log.Info("deployment", "namespace", ns, "name", name)
namespacedName := t1.NamespacedName{
Name: name,
Namespace: ns,
}

csm := new(csmv1.ContainerStorageModule)
ctx := context.Background()
err := r.Client.Get(ctx, namespacedName, csm)
if err != nil {
r.Log.Info("deployment get csm", "error", err.Error())
}

// get status and update csm

r.Log.Info("csm prev status", "state", csm.Status)

state, err := utils.CalculateState(ctx, csm, r, csm.GetCSMStatus())
if err != nil {
r.Log.Info("Failed to update Deployment status", "error", err.Error())
}
r.Log.Info("deployment status", "state", state)

if !state {
errorMsg := errors.New("deployment in error")
if err != nil {
errorMsg = err
}
err := utils.UpdateStatus(ctx, csm, r, r.Log, csm.GetCSMStatus())
if err != nil {
r.Log.Info("Failed to update Deployment status", "error", err.Error())
}
r.Log.Info("deployment in err", "err", errorMsg)

controllerstart := len(csm.Status.ControllerStatus.Starting)
controllerstop := len(csm.Status.ControllerStatus.Stopped)
r.EventRecorder.Eventf(csm, "Warning", "Updated", "Deployment status check csm, controller pod count starting:%d, stopped:%d", controllerstart, controllerstop)

r.EventRecorder.Eventf(csm, "Warning", "Updated", "Deployment status check Error ,controller pod desired:%d, available:%d", desired, available)
} else {
r.Log.Info("csm status", "curent state", csm.Status.State)
err := utils.UpdateStatus(ctx, csm, r, r.Log, csm.GetCSMStatus())
if err != nil {
r.Log.Info("Failed to update Deployment status", "error", err.Error())
}
r.EventRecorder.Eventf(csm, "Normal", "Updated", "Deployment status check OK : %s desired pods %d, ready pods %d", d.Name, desired, ready)
}
return
}

func (r *ContainerStorageModuleReconciler) handleDaemonsetUpdate(oldObj interface{}, obj interface{}) {
old, _ := oldObj.(*appsv1.DaemonSet)
d, _ := obj.(*appsv1.DaemonSet)
name := d.Spec.Template.Labels["csm"]
if name == "" {
r.Log.Info("daemonset not ours return", "name", d.Name)
r.Log.Info("daemonset not found", "name", d.Name)
return
}

Expand Down Expand Up @@ -253,14 +323,20 @@ func (r *ContainerStorageModuleReconciler) handleDaemonsetUpdate(oldObj interfac
r.Log.Info("csm prev status ", "state", csm.Status)

state, err := utils.CalculateState(ctx, csm, r, csm.GetCSMStatus())
if err != nil {
r.Log.Info("Failed to update Deployment status", "error", err.Error())
}
r.Log.Info("daemonset status", "state", state)

if !state {
errorMsg := errors.New("daemonset in error")
if err != nil {
errorMsg = err
}
_ = utils.UpdateStatus(ctx, csm, r, r.Log, csm.GetCSMStatus())
err := utils.UpdateStatus(ctx, csm, r, r.Log, csm.GetCSMStatus())
if err != nil {
r.Log.Info("Failed to update Daemonset status", "error", err.Error())
}
r.Log.Info("daemonset in err", "err", errorMsg)

nodestart := len(csm.Status.NodeStatus.Starting)
Expand All @@ -270,7 +346,10 @@ func (r *ContainerStorageModuleReconciler) handleDaemonsetUpdate(oldObj interfac
r.EventRecorder.Eventf(csm, "Warning", "Updated", "Daemonset status check Error ,node pod desired:%d, available:%d", desired, available)
} else {
r.Log.Info("csm status", "curent state", csm.Status.State)
_ = utils.UpdateStatus(ctx, csm, r, r.Log, csm.GetCSMStatus())
err := utils.UpdateStatus(ctx, csm, r, r.Log, csm.GetCSMStatus())
if err != nil {
r.Log.Info("Failed to update Daemonset status", "error", err.Error())
}
r.EventRecorder.Eventf(csm, "Normal", "Updated", "Daemonset status check OK : %s desired pods %d, ready pods %d", d.Name, desired, ready)
}
return
Expand All @@ -287,9 +366,13 @@ func (r *ContainerStorageModuleReconciler) ContentWatch() error {
sharedInformerFactory := sinformer.NewSharedInformerFactory(clientset, time.Duration(time.Hour))

contentInformer := sharedInformerFactory.Apps().V1().DaemonSets().Informer()
contentdeploymentInformer := sharedInformerFactory.Apps().V1().Deployments().Informer()
contentInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: r.handleDaemonsetUpdate,
})
contentdeploymentInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: r.handleDeploymentUpdate,
})

stop := make(chan struct{})
sharedInformerFactory.Start(stop)
Expand Down Expand Up @@ -434,7 +517,7 @@ func (r *ContainerStorageModuleReconciler) SyncCSM(ctx context.Context, cr csmv1
}

// Create/Update Deployment
err = deployment.SyncDeployment(ctx, &controller.Deployment, r.Client, reqLogger)
err = deployment.SyncDeployment(ctx, &controller.Deployment, r.Client, reqLogger, cr.Name)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ metadata:
data:
driver-config-params.yaml: |
CSI_LOG_LEVEL: debug
87 changes: 28 additions & 59 deletions pkg/drivers/powerscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,18 @@ func GetPowerScaleController(cr csmv1.ContainerStorageModule, operatorConfig uti
controllerYAML.Deployment.Spec.Replicas = &cr.Spec.Driver.Replicas

if len(cr.Spec.Driver.Controller.Tolerations) != 0 {
controllerYAML.Deployment.Spec.Template.Spec.Tolerations = cr.Spec.Driver.Controller.Tolerations
tols := make([]acorev1.TolerationApplyConfiguration, 0)
for _, t := range cr.Spec.Driver.Controller.Tolerations {
toleration := acorev1.Toleration()
toleration.WithEffect(t.Effect)
toleration.WithKey(t.Key)
toleration.WithValue(t.Value)
toleration.WithOperator(t.Operator)
toleration.WithTolerationSeconds(*t.TolerationSeconds)
tols = append(tols, *toleration)
}

controllerYAML.Deployment.Spec.Template.Spec.Tolerations = tols
}

if cr.Spec.Driver.Controller.NodeSelector != nil {
Expand All @@ -49,30 +60,36 @@ func GetPowerScaleController(cr csmv1.ContainerStorageModule, operatorConfig uti

containers := controllerYAML.Deployment.Spec.Template.Spec.Containers
for i, c := range containers {
if string(c.Name) == "driver" {
env := utils.ReplaceAllEnvs(c.Env, cr.Spec.Driver.Common.Envs)
containers[i].Env = utils.ReplaceAllEnvs(env, cr.Spec.Driver.Controller.Envs)
if string(*c.Name) == "driver" {
containers[i].Env = utils.ReplaceAllApplyCustomEnvs(c.Env, cr.Spec.Driver.Common.Envs, cr.Spec.Driver.Controller.Envs)
for _, e := range containers[i].Env {
if e.Value != nil {
//Log.Info("resolved 2 ", "env", *e.Name, "value", *e.Value)
}
}
if string(cr.Spec.Driver.Common.Image) != "" {
containers[i].Image = string(cr.Spec.Driver.Common.Image)
image := string(cr.Spec.Driver.Common.Image)
containers[i].Image = &image
}
}

c = utils.ReplaceALLContainerImage(operatorConfig.K8sVersion, c)
containers[i] = utils.UpdateSideCar(cr.Spec.Driver.SideCars, c)
tmp := utils.ReplaceALLContainerImageApply(operatorConfig.K8sVersion, containers[i])
containers[i] = utils.UpdateSideCarApply(cr.Spec.Driver.SideCars, tmp)

}

controllerYAML.Deployment.Spec.Template.Spec.Containers = containers
// Update volumes
for i, v := range controllerYAML.Deployment.Spec.Template.Spec.Volumes {
if v.Name == "certs" {
newV, err := getCertVolume(cr)
if *v.Name == "certs" {
newV, err := getApplyCertVolume(cr)
if err != nil {
return nil, err
}
controllerYAML.Deployment.Spec.Template.Spec.Volumes[i] = *newV
}
if v.Name == cr.Name+"-creds" && cr.Spec.Driver.AuthSecret != "" {
controllerYAML.Deployment.Spec.Template.Spec.Volumes[i].Secret.SecretName = cr.Spec.Driver.AuthSecret
if *v.Name == cr.Name+"-creds" && cr.Spec.Driver.AuthSecret != "" {
controllerYAML.Deployment.Spec.Template.Spec.Volumes[i].Secret.SecretName = &cr.Spec.Driver.AuthSecret
}

}
Expand Down Expand Up @@ -262,54 +279,6 @@ func PrecheckPowerScale(ctx context.Context, cr *csmv1.ContainerStorageModule, r
return nil
}

func getCertVolume(cr csmv1.ContainerStorageModule) (*corev1.Volume, error) {
skipCertValid := false
certCount := 1
for _, env := range cr.Spec.Driver.Common.Envs {
if env.Name == "X_CSI_ISI_SKIP_CERTIFICATE_VALIDATION" {
b, err := strconv.ParseBool(env.Value)
if err != nil {
return nil, fmt.Errorf("%s is an invalid value for X_CSI_ISI_SKIP_CERTIFICATE_VALIDATION: %v", env.Value, err)
}
skipCertValid = b
}
if env.Name == "CERT_SECRET_COUNT" {
d, err := strconv.ParseInt(env.Value, 0, 8)
if err != nil {
return nil, fmt.Errorf("%s is an invalid value for CERT_SECRET_COUNT: %v", env.Value, err)
}
certCount = int(d)
}
}

volume := corev1.Volume{
Name: "certs",
VolumeSource: corev1.VolumeSource{
Projected: &corev1.ProjectedVolumeSource{
Sources: []corev1.VolumeProjection{},
},
},
}

if !skipCertValid {
for i := 0; i < certCount; i++ {
source := corev1.SecretProjection{
LocalObjectReference: corev1.LocalObjectReference{Name: fmt.Sprintf("%s-certs-%d", cr.Name, i)},
Items: []corev1.KeyToPath{
{
Key: fmt.Sprintf("cert-%d", i),
Path: fmt.Sprintf("cert-%d", i),
},
},
}
volume.VolumeSource.Projected.Sources = append(volume.VolumeSource.Projected.Sources, corev1.VolumeProjection{Secret: &source})

}
}

return &volume, nil
}

func getApplyCertVolume(cr csmv1.ContainerStorageModule) (*acorev1.VolumeApplyConfiguration, error) {
skipCertValid := false
certCount := 1
Expand Down
9 changes: 4 additions & 5 deletions pkg/modules/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
csmv1 "github.com/dell/csm-operator/api/v1alpha1"
utils "github.com/dell/csm-operator/pkg/utils"
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -261,16 +260,16 @@ func AuthInjectDaemonset(ds applyv1.DaemonSetApplyConfiguration, cr csmv1.Contai
}

// AuthInjectDeployment - inject authorization into deployment
func AuthInjectDeployment(dp appsv1.Deployment, cr csmv1.ContainerStorageModule, op utils.OperatorConfig) (*appsv1.Deployment, error) {
authModule, containerPtr, err := getAuthCR(cr, op)
func AuthInjectDeployment(dp applyv1.DeploymentApplyConfiguration, cr csmv1.ContainerStorageModule, op utils.OperatorConfig) (*applyv1.DeploymentApplyConfiguration, error) {
authModule, containerPtr, err := getAuthApplyCR(cr, op)
if err != nil {
return nil, err
}

container := *containerPtr
container = utils.UpdateSideCar(authModule.Components, container)
container = utils.UpdateSideCarApply(authModule.Components, container)

vols, err := getAuthVolumes(cr, op, authModule.Components[0])
vols, err := getAuthApplyVolumes(cr, op, authModule.Components[0])
if err != nil {
return nil, err
}
Expand Down
15 changes: 7 additions & 8 deletions pkg/modules/authorization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
drivers "github.com/dell/csm-operator/pkg/drivers"
utils "github.com/dell/csm-operator/pkg/utils"
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
applyv1 "k8s.io/client-go/applyconfigurations/apps/v1"
Expand Down Expand Up @@ -234,24 +233,24 @@ func TestAuthInjectDaemonset(t *testing.T) {
}
}
func TestAuthInjectDeployment(t *testing.T) {
correctlyInjected := func(dp appsv1.Deployment) error {
correctlyInjected := func(dp applyv1.DeploymentApplyConfiguration) error {
err := checkAnnotation(dp.Annotations)
if err != nil {
return err
}
err = checkVolumes(dp.Spec.Template.Spec.Volumes)
err = checkApplyVolumes(dp.Spec.Template.Spec.Volumes)
if err != nil {
return err
}
err = checkContainers(dp.Spec.Template.Spec.Containers)
err = checkApplyContainers(dp.Spec.Template.Spec.Containers)
if err != nil {
return err
}
return nil
}

tests := map[string]func(t *testing.T) (bool, appsv1.Deployment, utils.OperatorConfig, csmv1.ContainerStorageModule){
"success - greenfield injection": func(*testing.T) (bool, appsv1.Deployment, utils.OperatorConfig, csmv1.ContainerStorageModule) {
tests := map[string]func(t *testing.T) (bool, applyv1.DeploymentApplyConfiguration, utils.OperatorConfig, csmv1.ContainerStorageModule){
"success - greenfield injection": func(*testing.T) (bool, applyv1.DeploymentApplyConfiguration, utils.OperatorConfig, csmv1.ContainerStorageModule) {
customResource, err := getCustomResource()
if err != nil {
panic(err)
Expand All @@ -262,7 +261,7 @@ func TestAuthInjectDeployment(t *testing.T) {
}
return true, controllerYAML.Deployment, operatorConfig, customResource
},
"success - brownfiled injection": func(*testing.T) (bool, appsv1.Deployment, utils.OperatorConfig, csmv1.ContainerStorageModule) {
"success - brownfiled injection": func(*testing.T) (bool, applyv1.DeploymentApplyConfiguration, utils.OperatorConfig, csmv1.ContainerStorageModule) {
customResource, err := getCustomResource()
if err != nil {
panic(err)
Expand All @@ -279,7 +278,7 @@ func TestAuthInjectDeployment(t *testing.T) {

return true, *newDeployment, operatorConfig, customResource
},
"fail - bad config path": func(*testing.T) (bool, appsv1.Deployment, utils.OperatorConfig, csmv1.ContainerStorageModule) {
"fail - bad config path": func(*testing.T) (bool, applyv1.DeploymentApplyConfiguration, utils.OperatorConfig, csmv1.ContainerStorageModule) {
customResource, err := getCustomResource()
if err != nil {
panic(err)
Expand Down
Loading

0 comments on commit 259e452

Please sign in to comment.