in operator/controllers/operator/oapserverconfig_controller.go [155:242]
func (r *OAPServerConfigReconciler) OverlayStaticFile(ctx context.Context, log logr.Logger,
oapServerConfig *operatorv1alpha1.OAPServerConfig, deployment *apps.Deployment) (bool, error) {
changed := false
sort.Sort(SortByFileName(oapServerConfig.Spec.File))
newMd5Hash := MD5Hash(oapServerConfig.Spec.File)
configmap := core.ConfigMap{}
err := r.Client.Get(ctx, client.ObjectKey{Namespace: oapServerConfig.Namespace,
Name: oapServerConfig.Name}, &configmap)
if err != nil && !apierrors.IsNotFound(err) {
log.Error(err, "failed to get the static file configuration's configmap")
return changed, err
}
// if the configmap exist and the static configuration changed, then delete it
if !apierrors.IsNotFound(err) {
oldMd5Hash := configmap.Labels["md5-file"]
if oldMd5Hash != newMd5Hash {
changed = true
if err := r.Client.Delete(ctx, &configmap); err != nil {
log.Error(err, "faled to delete the static file configuration's configmap")
}
} else {
log.Info("file configuration keeps the same as before")
return changed, nil
}
}
data := make(map[string]string)
mounts := []core.VolumeMount{}
volume := core.Volume{
Name: oapServerConfig.Name,
VolumeSource: core.VolumeSource{
ConfigMap: &core.ConfigMapVolumeSource{
LocalObjectReference: core.LocalObjectReference{
Name: oapServerConfig.Name,
},
},
},
}
for _, f := range oapServerConfig.Spec.File {
mounts = append(mounts, core.VolumeMount{
MountPath: f.Path + "/" + f.Name,
Name: oapServerConfig.Name,
SubPath: f.Name,
})
data[f.Name] = f.Data
}
labels := make(map[string]string)
// set the version label
labels["version"] = oapServerConfig.Spec.Version
// set the configuration type
labels["oapServerConfig"] = "static"
// set the md5 value of the data
labels["md5-file"] = newMd5Hash
// create configmap for static files
configmap = core.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: oapServerConfig.Name,
Namespace: oapServerConfig.Namespace,
OwnerReferences: []metav1.OwnerReference{
metav1.OwnerReference{
APIVersion: oapServerConfig.APIVersion,
Kind: oapServerConfig.Kind,
Name: oapServerConfig.Name,
UID: oapServerConfig.UID,
},
},
Labels: labels,
},
Data: data,
}
if err := r.Client.Create(ctx, &configmap); err != nil {
log.Error(err, "failed to create static configuration configmap")
return changed, err
}
overlayDeployment := deployment
overlayDeployment.Spec.Template.Spec.Containers[0].VolumeMounts = mounts
overlayDeployment.Spec.Template.Spec.Volumes = []core.Volume{volume}
if err := kubernetes.ApplyOverlay(deployment, overlayDeployment); err != nil {
log.Error(err, "failed to apply overlay deployment")
}
log.Info("successfully overlay the file configuration")
return changed, nil
}