func()

in pkg/service/jenkins/jenkins.go [214:272]


func (j JenkinsServiceImpl) mountGerritCredentials(instance *jenkinsApi.Jenkins) error {
	options := client.ListOptions{Namespace: instance.Namespace}
	list := &gerritApi.GerritList{}

	if err := j.k8sClient.List(context.TODO(), list, &options); err != nil {
		str := err.Error()

		log.Info(str)
		log.V(1).Info(fmt.Sprintf("Gerrit installation is not found in namespace %v", instance.Namespace))

		return nil
	}

	if len(list.Items) == 0 {
		log.V(1).Info(fmt.Sprintf("Gerrit installation is not found in namespace %v", instance.Namespace))

		return nil
	}

	gerritCrObject := &list.Items[0]
	gerritSpecName := fmt.Sprintf(pathStringFormat, gerritSpec.EdpAnnotationsPrefix, gerritSpec.EdpCiUSerSshKeySuffix)

	if val, ok := gerritCrObject.ObjectMeta.Annotations[gerritSpecName]; ok {
		volMount := []coreV1Api.VolumeMount{
			{
				Name:      val,
				MountPath: sshKeyDefaultMountPath,
				ReadOnly:  true,
			},
		}

		var mode int32 = 400

		vol := []coreV1Api.Volume{
			{
				Name: val,
				VolumeSource: coreV1Api.VolumeSource{
					Secret: &coreV1Api.SecretVolumeSource{
						SecretName:  val,
						DefaultMode: &mode,
						Items: []coreV1Api.KeyToPath{
							{
								Key:  "id_rsa",
								Path: "id_rsa",
								Mode: &mode,
							},
						},
					},
				},
			},
		}

		if err := j.platformService.AddVolumeToInitContainer(instance, initContainerName, vol, volMount); err != nil {
			return fmt.Errorf("failed to patch Jenkins DC in namespace %v: %w", instance.Namespace, err)
		}
	}

	return nil
}