Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(operator): consider corner cases in KACR controller #1270

Merged
merged 7 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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