func()

in operator/controllers/operator/oapserverdynamicconfig_controller.go [111:217]


func (r *OAPServerDynamicConfigReconciler) UpdateDynamicConfig(ctx context.Context, log logr.Logger,
	oapServer *operatorv1alpha1.OAPServer, config *operatorv1alpha1.OAPServerDynamicConfig) error {
	changed := false
	exsitedConfiguration := map[string]bool{}

	dynamicConfigList := operatorv1alpha1.OAPServerDynamicConfigList{}
	opts := []client.ListOption{
		client.InNamespace(config.Namespace),
	}
	if err := r.List(ctx, &dynamicConfigList, opts...); err != nil && !apierrors.IsNotFound(err) {
		return fmt.Errorf("failed to list OAPServerDynamicConfig: %w", err)
	}
	for _, i := range dynamicConfigList.Items {
		if i.Name != config.Name {
			for _, n := range i.Spec.Data {
				exsitedConfiguration[n.Name] = true
			}
		}
	}

	configuration := config.Spec.Data
	for i := range configuration {
		_, ok := exsitedConfiguration[configuration[i].Name]
		if ok {
			return fmt.Errorf("the configuration %s already exist", configuration[i].Name)
		}
	}
	sort.Sort(SortByConfigName(configuration))
	newMd5Hash := MD5Hash(configuration)

	labelSelector, err := labels.Parse(config.Spec.LabelSelector)
	if err != nil {
		log.Error(err, "failed to parse string to labelselector")
		return err
	}
	label, err := labels.ConvertSelectorToLabelsMap(config.Spec.LabelSelector)
	if err != nil {
		log.Error(err, "failed to convert labelselector to map")
		return err
	}

	configmap := core.ConfigMap{}
	err = r.Client.Get(ctx, client.ObjectKey{Namespace: config.Namespace,
		Name: config.Name}, &configmap)
	if err != nil && !apierrors.IsNotFound(err) {
		log.Error(err, "failed to get the dynamic configuration configmap")
		return err
	}
	// if the configmap exist and the dynamic configuration or labelselector changed, then delete it
	if !apierrors.IsNotFound(err) {
		oldMd5Hash := configmap.Labels["md5"]
		// check dynamic configuration
		if oldMd5Hash != newMd5Hash {
			changed = true
		}

		// check labelselector
		if !labelSelector.Matches(labels.Set(configmap.Labels)) {
			changed = true
		}
		if changed {
			if err := r.Client.Delete(ctx, &configmap); err != nil {
				log.Error(err, "faled to delete the dynamic configuration configmap")
			}
		} else {
			log.Info("dynamic configuration keeps the same as before")
			return nil
		}
	}

	// set the version label
	label["version"] = config.Spec.Version
	// set the configuration type
	label["OAPServerConfig"] = "dynamic"
	// set the md5 value
	label["md5"] = newMd5Hash
	// set the data
	data := map[string]string{}
	for _, v := range configuration {
		data[v.Name] = v.Value
	}
	configmap = core.ConfigMap{
		ObjectMeta: metav1.ObjectMeta{
			Name:      config.Name,
			Namespace: oapServer.Namespace,
			OwnerReferences: []metav1.OwnerReference{
				metav1.OwnerReference{
					APIVersion: config.APIVersion,
					Kind:       config.Kind,
					Name:       config.Name,
					UID:        config.UID,
				},
			},
			Labels: label,
		},
		Data: data,
	}

	// create the dynamic configuration configmap
	if err := r.Client.Create(ctx, &configmap); err != nil {
		log.Error(err, "failed to create dynamic configuration configmap")
		return err
	}

	log.Info("successfully setup the dynamic configuration")
	return nil
}