github.com/interconnectedcloud/qdr-operator@v0.0.0-20210826174505-576d2b33dac7/pkg/resources/deployments/deployment.go (about)

     1  package deployments
     2  
     3  import (
     4  	"strconv"
     5  
     6  	v1alpha1 "github.com/interconnectedcloud/qdr-operator/pkg/apis/interconnectedcloud/v1alpha1"
     7  	"github.com/interconnectedcloud/qdr-operator/pkg/resources/containers"
     8  	"github.com/interconnectedcloud/qdr-operator/pkg/utils/configs"
     9  	"github.com/interconnectedcloud/qdr-operator/pkg/utils/selectors"
    10  	appsv1 "k8s.io/api/apps/v1"
    11  	corev1 "k8s.io/api/core/v1"
    12  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    13  )
    14  
    15  // move this to util
    16  // Set labels in a map
    17  func labelsForInterconnect(name string) map[string]string {
    18  	return map[string]string{
    19  		selectors.LabelAppKey:      name,
    20  		selectors.LabelResourceKey: name,
    21  	}
    22  }
    23  
    24  func CheckDeployedContainer(actual *corev1.PodTemplateSpec, cr *v1alpha1.Interconnect) bool {
    25  	desired := containers.ContainerForInterconnect(cr)
    26  	if len(actual.Spec.Containers) != 1 || !containers.CheckInterconnectContainer(&desired, &actual.Spec.Containers[0]) {
    27  		actual.Spec.Containers = []corev1.Container{desired}
    28  		return false
    29  	}
    30  	return true
    31  }
    32  
    33  // Create NewDeploymentForCR method to create deployment
    34  func NewDeploymentForCR(m *v1alpha1.Interconnect) *appsv1.Deployment {
    35  	labels := selectors.LabelsForInterconnect(m.Name)
    36  	replicas := m.Spec.DeploymentPlan.Size
    37  	affinity := &corev1.Affinity{}
    38  	if m.Spec.DeploymentPlan.Placement == v1alpha1.PlacementAntiAffinity {
    39  		affinity = &corev1.Affinity{
    40  			PodAntiAffinity: &corev1.PodAntiAffinity{
    41  				RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{
    42  					{
    43  						LabelSelector: &metav1.LabelSelector{
    44  							MatchExpressions: []metav1.LabelSelectorRequirement{
    45  								{
    46  									Key:      "application",
    47  									Operator: metav1.LabelSelectorOpIn,
    48  									Values:   []string{m.Name},
    49  								},
    50  							},
    51  						},
    52  						TopologyKey: "kubernetes.io/hostname",
    53  					},
    54  				},
    55  			},
    56  		}
    57  	}
    58  	dep := &appsv1.Deployment{
    59  		TypeMeta: metav1.TypeMeta{
    60  			APIVersion: "apps/v1",
    61  			Kind:       "Deployment",
    62  		},
    63  		ObjectMeta: metav1.ObjectMeta{
    64  			Name:      m.Name,
    65  			Namespace: m.Namespace,
    66  		},
    67  		Spec: appsv1.DeploymentSpec{
    68  			Replicas: &replicas,
    69  			Selector: &metav1.LabelSelector{
    70  				MatchLabels: labels,
    71  			},
    72  			Template: corev1.PodTemplateSpec{
    73  				ObjectMeta: metav1.ObjectMeta{
    74  					Labels: labels,
    75  					Annotations: map[string]string{
    76  						"prometheus.io/port":   strconv.Itoa(int(m.Spec.DeploymentPlan.LivenessPort)),
    77  						"prometheus.io/scrape": "true",
    78  					},
    79  				},
    80  				Spec: corev1.PodSpec{
    81  					ServiceAccountName: m.Name,
    82  					Affinity:           affinity,
    83  					Containers:         []corev1.Container{containers.ContainerForInterconnect(m)},
    84  				},
    85  			},
    86  		},
    87  	}
    88  	volumes := []corev1.Volume{}
    89  	for _, profile := range m.Spec.SslProfiles {
    90  		if len(profile.Credentials) > 0 {
    91  			volumes = append(volumes, corev1.Volume{
    92  				Name: profile.Credentials,
    93  				VolumeSource: corev1.VolumeSource{
    94  					Secret: &corev1.SecretVolumeSource{
    95  						SecretName: profile.Credentials,
    96  					},
    97  				},
    98  			})
    99  		}
   100  		if len(profile.CaCert) > 0 && profile.CaCert != profile.Credentials {
   101  			volumes = append(volumes, corev1.Volume{
   102  				Name: profile.CaCert,
   103  				VolumeSource: corev1.VolumeSource{
   104  					Secret: &corev1.SecretVolumeSource{
   105  						SecretName: profile.CaCert,
   106  					},
   107  				},
   108  			})
   109  		}
   110  	}
   111  	if len(m.Spec.Users) > 0 {
   112  		volumes = append(volumes, corev1.Volume{
   113  			Name: "sasl-users",
   114  			VolumeSource: corev1.VolumeSource{
   115  				Secret: &corev1.SecretVolumeSource{
   116  					SecretName: m.Spec.Users,
   117  				},
   118  			},
   119  		})
   120  		volumes = append(volumes, corev1.Volume{
   121  			Name: "sasl-config",
   122  			VolumeSource: corev1.VolumeSource{
   123  				ConfigMap: &corev1.ConfigMapVolumeSource{
   124  					LocalObjectReference: corev1.LocalObjectReference{
   125  						Name: m.Name + "-sasl-config",
   126  					},
   127  				},
   128  			},
   129  		})
   130  	}
   131  
   132  	dep.Spec.Template.Spec.Volumes = volumes
   133  
   134  	return dep
   135  }
   136  
   137  // Create NewDaemonSetForCR method to create daemonset
   138  func NewDaemonSetForCR(m *v1alpha1.Interconnect) *appsv1.DaemonSet {
   139  	labels := selectors.LabelsForInterconnect(m.Name)
   140  
   141  	ds := &appsv1.DaemonSet{
   142  		TypeMeta: metav1.TypeMeta{
   143  			APIVersion: "apps/v1",
   144  			Kind:       "DaemonSet",
   145  		},
   146  		ObjectMeta: metav1.ObjectMeta{
   147  			Name:      m.Name,
   148  			Namespace: m.Namespace,
   149  		},
   150  		Spec: appsv1.DaemonSetSpec{
   151  			Selector: &metav1.LabelSelector{
   152  				MatchLabels: labels,
   153  			},
   154  			Template: corev1.PodTemplateSpec{
   155  				ObjectMeta: metav1.ObjectMeta{
   156  					Labels: labels,
   157  					Annotations: map[string]string{
   158  						"prometheus.io/port":   strconv.Itoa(int(m.Spec.DeploymentPlan.LivenessPort)),
   159  						"prometheus.io/scrape": "true",
   160  					},
   161  				},
   162  				Spec: corev1.PodSpec{
   163  					ServiceAccountName: m.Name,
   164  					Containers:         []corev1.Container{containers.ContainerForInterconnect(m)},
   165  				},
   166  			},
   167  		},
   168  	}
   169  	volumes := []corev1.Volume{}
   170  	for _, profile := range m.Spec.SslProfiles {
   171  		if len(profile.Credentials) > 0 {
   172  			volumes = append(volumes, corev1.Volume{
   173  				Name: profile.Credentials,
   174  				VolumeSource: corev1.VolumeSource{
   175  					Secret: &corev1.SecretVolumeSource{
   176  						SecretName: profile.Credentials,
   177  					},
   178  				},
   179  			})
   180  		}
   181  		if len(profile.CaCert) > 0 && configs.IsCaSecretNeeded(&profile) {
   182  			volumes = append(volumes, corev1.Volume{
   183  				Name: profile.CaCert,
   184  				VolumeSource: corev1.VolumeSource{
   185  					Secret: &corev1.SecretVolumeSource{
   186  						SecretName: profile.CaCert,
   187  					},
   188  				},
   189  			})
   190  		}
   191  	}
   192  	if len(m.Spec.Users) > 0 {
   193  		volumes = append(volumes, corev1.Volume{
   194  			Name: "sasl-users",
   195  			VolumeSource: corev1.VolumeSource{
   196  				Secret: &corev1.SecretVolumeSource{
   197  					SecretName: m.Spec.Users,
   198  				},
   199  			},
   200  		})
   201  		volumes = append(volumes, corev1.Volume{
   202  			Name: "sasl-config",
   203  			VolumeSource: corev1.VolumeSource{
   204  				ConfigMap: &corev1.ConfigMapVolumeSource{
   205  					LocalObjectReference: corev1.LocalObjectReference{
   206  						Name: m.Name + "-sasl-config",
   207  					},
   208  				},
   209  			},
   210  		})
   211  	}
   212  
   213  	ds.Spec.Template.Spec.Volumes = volumes
   214  
   215  	return ds
   216  }