// Handle Jenkins Configuration-as-Code configuration
Handle Jenkins Configuration-as-Code configuration
// Handle Jenkins Configuration - as - Code configuration
func handleJenkinsCasCConfig(c *Controller, providedConfig map[string]string, namespace string) error {
	jenkinsCasCConfig, err := c.configmapLister.ConfigMaps(namespace).Get(jenkinsCasCConfigName)
	if err != nil {
		if errors.IsNotFound(err) {
			return nil
		}
		klog.Errorf("could not find %s/%s", namespace, jenkinsCasCConfigName)
		return err
	}
	jenkinsCascConfigToUpdate := jenkinsCasCConfig.DeepCopy()
	cascMap := make(map[string]interface{})
	jenkinsCasCConfigTemplate, err := getOrCreateJenkinsCasCTemplate(c.configMapClient, namespace, jenkinsCascConfigToUpdate.Data[jenkinsYamlKey])
	if err != nil {
		return err
	}
	if strings.Compare(providedConfig[resourceLimitKey], string(customLimit)) == 0 {
		if customJenkinsYaml, ok := providedConfig[jenkinsYamlKey]; ok {
			jenkinsCasCConfigTemplate = customJenkinsYaml
		}
	}
	err = yaml.Unmarshal([]byte(jenkinsCasCConfigTemplate), &cascMap)
	if err != nil {
		return err
	}
	kubernetesMap := cascMap["jenkins"].(map[interface{}]interface{})["clouds"].([]interface{})[0].(map[interface{}]interface{})["kubernetes"]
	concurrent, ok := providedConfig[podConcurrentKey]
	if ok {
		kubernetesMap.(map[interface{}]interface{})["containerCapStr"] = concurrent
	}
	if templates, ok := kubernetesMap.(map[interface{}]interface{})["templates"].([]interface{}); ok {
		for _, template := range templates {
			if template, ok := template.(map[interface{}]interface{}); ok {
				if name, ok := template["name"]; ok {
					if containers, ok := template["containers"]; ok {
						switch name {
						case "base":
							setContainersLimit(providedConfig, containers, "base")
						case "nodejs":
							setContainersLimit(providedConfig, containers, "nodejs")
						case "maven":
							setContainersLimit(providedConfig, containers, "maven")
						case "go":
							setContainersLimit(providedConfig, containers, "go")
						}
					}
				}
			}
		}
	}
	jenkinsYaml, err := yaml.Marshal(cascMap)
	if err != nil {
		return err
	}
	jenkinsCascConfigToUpdate.Data[jenkinsYamlKey] = string(jenkinsYaml)
	_, err = c.configMapClient.ConfigMaps(namespace).Update(context.Background(), jenkinsCascConfigToUpdate, metav1.UpdateOptions{})
	if err != nil {
		klog.Errorf("failed to update configmap: %s/%s", namespace, jenkinsCasCConfigName)
		return err
	}
	klog.V(5).Infof("updated configmap %s/%s successfully", namespace, jenkinsCasCConfigName)
	return nil
}
func handleJenkinsCasCConfig ( c * Controller , providedConfig map [ string ] string , namespace string ) error { jenkinsCasCConfig , err := c . configmapLister . ConfigMaps ( namespace ) . Get ( jenkinsCasCConfigName ) if err != nil { if errors . IsNotFound ( err ) { return nil } klog . Errorf ( " " , namespace , jenkinsCasCConfigName ) return err } jenkinsCascConfigToUpdate := jenkinsCasCConfig . DeepCopy ( ) cascMap := make ( map [ string ] interface { } ) jenkinsCasCConfigTemplate , err := getOrCreateJenkinsCasCTemplate ( c . configMapClient , namespace , jenkinsCascConfigToUpdate . Data [ jenkinsYamlKey ] ) if err != nil { return err } if strings . Compare ( providedConfig [ resourceLimitKey ] , string ( customLimit ) ) == 0 { if customJenkinsYaml , ok := providedConfig [ jenkinsYamlKey ] ; ok { jenkinsCasCConfigTemplate = customJenkinsYaml } } err = yaml . Unmarshal ( [ ] byte ( jenkinsCasCConfigTemplate ) , & cascMap ) if err != nil { return err } kubernetesMap := cascMap [ " " ] . ( map [ interface { } ] interface { } ) [ " " ] . ( [ ] interface { } ) [ 0 ] . ( map [ interface { } ] interface { } ) [ " " ] concurrent , ok := providedConfig [ podConcurrentKey ] if ok { kubernetesMap . ( map [ interface { } ] interface { } ) [ " " ] = concurrent } if templates , ok := kubernetesMap . ( map [ interface { } ] interface { } ) [ " " ] . ( [ ] interface { } ) ; ok { for _ , template := range templates { if template , ok := template . ( map [ interface { } ] interface { } ) ; ok { if name , ok := template [ " " ] ; ok { if containers , ok := template [ " " ] ; ok { switch name { case " " : setContainersLimit ( providedConfig , containers , " " ) case " " : setContainersLimit ( providedConfig , containers , " " ) case " " : setContainersLimit ( providedConfig , containers , " " ) case " " : setContainersLimit ( providedConfig , containers , " " ) } } } } } } jenkinsYaml , err := yaml . Marshal ( cascMap ) if err != nil { return err } jenkinsCascConfigToUpdate . Data [ jenkinsYamlKey ] = string ( jenkinsYaml ) _ , err = c . configMapClient . ConfigMaps ( namespace ) . Update ( context . Background ( ) , jenkinsCascConfigToUpdate , metav1 . UpdateOptions { } ) if err != nil { klog . Errorf ( " " , namespace , jenkinsCasCConfigName ) return err } klog . V ( 5 ) . Infof ( " " , namespace , jenkinsCasCConfigName ) return nil }
Handle Jenkins Configuration-as-Code configuration
Handle Jenkins Configuration - as - Code configuration
// get CasC configuration // overwrite Jenkins CasC config template only while custom resource limit set // set concurrent // set pod template // type safe check // ensure template name exist // handle base pod template // handle nodejs pod template // handle maven pod template // handle go pod template // set casc config into newJenkinsCascConfig // Update jenkinsconfig configmap