Skip to content

Commit

Permalink
feat(operator): consider corner cases in KACR controller (#1270)
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Bacher <[email protected]>
  • Loading branch information
bacherfl authored Apr 25, 2023
1 parent 706292a commit b3b7010
Show file tree
Hide file tree
Showing 7 changed files with 417 additions and 10 deletions.
5 changes: 5 additions & 0 deletions operator/apis/lifecycle/v1alpha3/keptnworkload_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha3

import (
"fmt"
"strings"

"github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3/common"
Expand Down Expand Up @@ -117,3 +118,7 @@ func (w KeptnWorkload) GetEventAnnotations() map[string]string {
"workloadVersion": w.Spec.Version,
}
}

func (w KeptnWorkload) GetNameWithoutAppPrefix() string {
return strings.TrimPrefix(w.Name, fmt.Sprintf("%s-", w.Spec.AppName))
}
44 changes: 44 additions & 0 deletions operator/apis/lifecycle/v1alpha3/keptnworkload_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package v1alpha3

import (
"testing"

"github.com/stretchr/testify/require"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestKeptnWorkload_GetNameWithoutAppPrefix(t *testing.T) {
type fields struct {
ObjectMeta v1.ObjectMeta
Spec KeptnWorkloadSpec
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "remove app prefix",
fields: fields{
ObjectMeta: v1.ObjectMeta{
Name: "my-app-my-workload",
},
Spec: KeptnWorkloadSpec{
AppName: "my-app",
},
},
want: "my-workload",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := KeptnWorkload{
ObjectMeta: tt.fields.ObjectMeta,
Spec: tt.fields.Spec,
}
got := w.GetNameWithoutAppPrefix()

require.Equal(t, tt.want, got)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,23 @@ func (r *KeptnAppCreationRequestReconciler) SetupWithManager(mgr ctrl.Manager) e

func (r *KeptnAppCreationRequestReconciler) updateKeptnApp(ctx context.Context, keptnApp *lifecycle.KeptnApp, workloads *lifecycle.KeptnWorkloadList) error {

updated := false
addOrUpdatedWorkload := r.addOrUpdateWorkloads(workloads, keptnApp)
removedWorkload := r.cleanupWorkloads(workloads, keptnApp)

if !addOrUpdatedWorkload && !removedWorkload {
return nil
}

keptnApp.Spec.Version = computeVersionFromWorkloads(workloads.Items)

return r.Update(ctx, keptnApp)
}

func (r *KeptnAppCreationRequestReconciler) addOrUpdateWorkloads(workloads *lifecycle.KeptnWorkloadList, keptnApp *lifecycle.KeptnApp) bool {
updated := false
for _, workload := range workloads.Items {
foundWorkload := false
workloadName := strings.TrimPrefix(workload.Name, fmt.Sprintf("%s-", keptnApp.Name))
workloadName := workload.GetNameWithoutAppPrefix()
for index, appWorkload := range keptnApp.Spec.Workloads {
if appWorkload.Name == workloadName {
// make sure the version matches the current version of the workload
Expand All @@ -190,14 +202,27 @@ func (r *KeptnAppCreationRequestReconciler) updateKeptnApp(ctx context.Context,
updated = true
}
}
return updated
}

if !updated {
return nil
}

keptnApp.Spec.Version = computeVersionFromWorkloads(workloads.Items)
func (r *KeptnAppCreationRequestReconciler) cleanupWorkloads(workloads *lifecycle.KeptnWorkloadList, keptnApp *lifecycle.KeptnApp) bool {
updated := false
updatedWorkloads := []lifecycle.KeptnWorkloadRef{}
for index, appWorkload := range keptnApp.Spec.Workloads {
foundWorkload := false
for _, workload := range workloads.Items {
if appWorkload.Name == workload.GetNameWithoutAppPrefix() {
updatedWorkloads = append(updatedWorkloads, keptnApp.Spec.Workloads[index])
break
}
}

return r.Update(ctx, keptnApp)
if !foundWorkload {
updated = true
}
}
keptnApp.Spec.Workloads = updatedWorkloads
return updated
}

func (r *KeptnAppCreationRequestReconciler) createKeptnApp(ctx context.Context, creationRequest *lifecycle.KeptnAppCreationRequest, workloads *lifecycle.KeptnWorkloadList) error {
Expand Down
Loading

0 comments on commit b3b7010

Please sign in to comment.