Skip to content

Commit

Permalink
Support dedicated ConfigMap created by other means
Browse files Browse the repository at this point in the history
To give the user the possibility of creating the configMap to be attached to the
Stack by themselves, Stackset Controller checks if there's a reference in the
ConfigurationResources before creating the versioned ConfigMap.
If there isn't, the existing resource named on ConfigurationResources.Name is
updated with the Stack info on ownerReferences.
  • Loading branch information
katyanna committed Nov 1, 2023
1 parent 510c895 commit 58cda7a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 13 deletions.
34 changes: 27 additions & 7 deletions controller/stack_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,16 @@ func (c *StackSetController) updateStackConfigMap(
// create their versions owned by the Stack, deleting the template used after each
// version creation.
//
// If the Stack already has the same amount of versioned ConfigMaps as defined in the
// StackTemplate, the templates are deleted and the Reconcile method is exited before
// the version creation loop.
// If configMapRef is not defined, the Stackset Controller expects the
// ConfigurationResources.Name to be the somehow else created ConfigMap to be attached
// to the Stack and just updates it with the ownerReferences.
//
// Update of the versioned ConfigMaps is not allowed, any change to the resource must
// implicate the deployment of a new Stack.
// If the Stack already has the same amount of versioned ConfigMaps as defined in the
// StackTemplate, the Reconcile method is exited before the resource update/creation
// loop.
//
// The deletion of the templates intends to keep a clean environment with no unused
// resouces, while ensuring that it'll will be used only for its intended Stack.
// Update of versioned ConfigMaps is not encouraged, but is allowed considering
// emergency needs.
func (c *StackSetController) ReconcileStackConfigMap(
ctx context.Context,
stack *zv1.Stack,
Expand All @@ -405,6 +406,10 @@ func (c *StackSetController) ReconcileStackConfigMap(

configMaps := make(map[string]string)
for _, configMap := range stack.Spec.ConfigurationResources {
if configMap.ConfigMapRef == nil {
configMaps[configMap.Name] = configMap.Name
continue
}
templateName := configMap.ConfigMapRef.Name
configMaps[templateName] = generateConfigMapName(stack, templateName)
}
Expand All @@ -430,6 +435,21 @@ func (c *StackSetController) ReconcileStackConfigMap(
return err
}

existingConfigMap, _ := c.client.CoreV1().ConfigMaps(stack.Namespace).Get(ctx, configMap.Name, metav1.GetOptions{})
if existingConfigMap != nil {
_, err = c.client.CoreV1().ConfigMaps(configMap.Namespace).Update(ctx, configMap, metav1.UpdateOptions{})
if err != nil {
return err
}
c.recorder.Eventf(
stack,
apiv1.EventTypeNormal,
"UpdatedConfigMap",
"Updated ConfigMap %s",
configMap.Name,
)
continue
}
_, err = c.client.CoreV1().ConfigMaps(configMap.Namespace).Create(ctx, configMap, metav1.CreateOptions{})
if err != nil {
return err
Expand Down
56 changes: 53 additions & 3 deletions controller/stack_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -958,12 +958,40 @@ func TestReconcileStackRouteGroup(t *testing.T) {
}

func TestReconcileStackConfigMap(t *testing.T) {
singleNamedConfigMapStack := baseTestStack
singleNamedConfigMapStack.Spec = zv1.StackSpecInternal{
StackSpec: zv1.StackSpec{
ConfigurationResources: []zv1.ConfigurationResourcesSpec{
{
Name: "foo-v1-single-configmap",
},
},
PodTemplate: zv1.PodTemplateSpec{
Spec: v1.PodSpec{
Volumes: []v1.Volume{
{
Name: "configmap",
VolumeSource: v1.VolumeSource{
ConfigMap: &v1.ConfigMapVolumeSource{
LocalObjectReference: v1.LocalObjectReference{
Name: "foo-v1-single-configmap",
},
},
},
},
},
},
},
},
}

singleConfigMapStack := baseTestStack
singleConfigMapStack.Spec = zv1.StackSpecInternal{
StackSpec: zv1.StackSpec{
ConfigurationResources: []zv1.ConfigurationResourcesSpec{
{
ConfigMapRef: v1.LocalObjectReference{
Name: "single-configmap",
ConfigMapRef: &v1.LocalObjectReference{
Name: "single-configmap",
},
},
Expand Down Expand Up @@ -1028,12 +1056,14 @@ func TestReconcileStackConfigMap(t *testing.T) {
StackSpec: zv1.StackSpec{
ConfigurationResources: []zv1.ConfigurationResourcesSpec{
{
ConfigMapRef: v1.LocalObjectReference{
Name: "first-configmap",
ConfigMapRef: &v1.LocalObjectReference{
Name: "first-configmap",
},
},
{
ConfigMapRef: v1.LocalObjectReference{
Name: "scnd-configmap",
ConfigMapRef: &v1.LocalObjectReference{
Name: "scnd-configmap",
},
},
Expand Down Expand Up @@ -1103,6 +1133,26 @@ func TestReconcileStackConfigMap(t *testing.T) {
template []*v1.ConfigMap
expected []*v1.ConfigMap
}{
{
name: "configmap ownerReference is added to already named deployed configmap",
stack: singleNamedConfigMapStack,
existing: nil,
template: []*v1.ConfigMap{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo-v1-single-configmap",
Namespace: singleNamedConfigMapStack.Namespace,
},
Data: baseData,
},
},
expected: []*v1.ConfigMap{
{
ObjectMeta: singleConfigMapMetaObj,
Data: baseData,
},
},
},
{
name: "configmap version is created, mounted as volume",
stack: singleConfigMapStack,
Expand Down
4 changes: 4 additions & 0 deletions docs/stack_crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ spec:
TODO: Add other useful fields. apiVersion, kind, uid?'
type: string
type: object
name:
type: string
required:
- name
type: object
type: array
externalIngress:
Expand Down
4 changes: 4 additions & 0 deletions docs/stackset_crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,10 @@ spec:
uid?'
type: string
type: object
name:
type: string
required:
- name
type: object
type: array
minReadySeconds:
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/zalando.org/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,9 @@ type StackSpec struct {
// ConfigurationResourcesSpec makes it possible to defined the config resources to be created
// +k8s:deepcopy-gen=true
type ConfigurationResourcesSpec struct {
Name string `json:"name"`
// ConfigMap to be versioned for Stack
ConfigMapRef v1.LocalObjectReference `json:"configMapRef,omitempty"`
ConfigMapRef *v1.LocalObjectReference `json:"configMapRef,omitempty"`
}

// StackSpecInternal is the spec part of the Stack, including `ingress` and
Expand Down
10 changes: 8 additions & 2 deletions pkg/apis/zalando.org/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 58cda7a

Please sign in to comment.