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): rework Workload and Application span structure #452

Merged
merged 12 commits into from
Nov 24, 2022
48 changes: 47 additions & 1 deletion operator/api/v1alpha1/common/phases.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package common

import "strings"
import (
"strings"

"go.opentelemetry.io/otel/propagation"
)

type KeptnPhase KeptnPhaseType

Expand Down Expand Up @@ -33,6 +37,37 @@ func (p KeptnPhaseType) IsPostTask() bool {
return strings.Contains(p.ShortName, "PostDeployTasks")
}

func GetShortPhaseName(phase string) string {
var phases = []KeptnPhaseType{
PhaseWorkloadPreDeployment,
PhaseWorkloadPostDeployment,
PhaseWorkloadPreEvaluation,
PhaseWorkloadPostEvaluation,
PhaseWorkloadDeployment,
PhaseAppPreDeployment,
PhaseAppPostDeployment,
PhaseAppPreEvaluation,
PhaseAppPostEvaluation,
PhaseAppDeployment,
PhaseCompleted,
PhaseCancelled,
}
odubajDT marked this conversation as resolved.
Show resolved Hide resolved

for _, p := range phases {
if phase == p.ShortName {
return p.ShortName
}
}

for _, p := range phases {
if phase == p.LongName {
return p.ShortName
}
}

return ""
}

var (
PhaseWorkloadPreDeployment = KeptnPhaseType{LongName: "Workload Pre-Deployment Tasks", ShortName: "WorkloadPreDeployTasks"}
PhaseWorkloadPostDeployment = KeptnPhaseType{LongName: "Workload Post-Deployment Tasks", ShortName: "WorkloadPostDeployTasks"}
Expand All @@ -47,3 +82,14 @@ var (
PhaseCompleted = KeptnPhaseType{LongName: "Completed", ShortName: "Completed"}
PhaseCancelled = KeptnPhaseType{LongName: "Cancelled", ShortName: "Cancelled"}
)

type PhaseTraceID map[string]propagation.MapCarrier

func (pid PhaseTraceID) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) {
pid[GetShortPhaseName(phase)] = carrier

}

func (pid PhaseTraceID) GetPhaseTraceID(phase string) propagation.MapCarrier {
return pid[GetShortPhaseName(phase)]
}
46 changes: 46 additions & 0 deletions operator/api/v1alpha1/common/phases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/propagation"
)

func TestKeptnPhaseType_IsEvaluation(t *testing.T) {
Expand Down Expand Up @@ -227,3 +228,48 @@ func TestKeptnPhaseType_IsPostTask(t *testing.T) {
})
}
}

func TestPhaseTraceID(t *testing.T) {
trace := PhaseTraceID{}

trace.SetPhaseTraceID(PhaseAppDeployment.LongName, propagation.MapCarrier{
"name": "trace",
"name2": "trace2",
})

require.Equal(t, PhaseTraceID{
PhaseAppDeployment.ShortName: propagation.MapCarrier{
"name": "trace",
"name2": "trace2",
},
}, trace)

trace.SetPhaseTraceID(PhaseWorkloadDeployment.ShortName, propagation.MapCarrier{
"name3": "trace3",
})

require.Equal(t, PhaseTraceID{
PhaseAppDeployment.ShortName: propagation.MapCarrier{
"name": "trace",
"name2": "trace2",
},
PhaseWorkloadDeployment.ShortName: propagation.MapCarrier{
"name3": "trace3",
},
}, trace)

require.Equal(t, propagation.MapCarrier{
"name": "trace",
"name2": "trace2",
}, trace.GetPhaseTraceID(PhaseAppDeployment.LongName))

require.Equal(t, propagation.MapCarrier{
"name3": "trace3",
}, trace.GetPhaseTraceID(PhaseWorkloadDeployment.ShortName))
}

func TestGetShortPhaseName(t *testing.T) {
require.Equal(t, "WorkloadPreDeployTasks", GetShortPhaseName("WorkloadPreDeployTasks"))
require.Equal(t, "WorkloadPreDeployTasks", GetShortPhaseName("Workload Pre-Deployment Tasks"))
require.Equal(t, "", GetShortPhaseName("Workload Pre-Deploycdddment Tasks"))
}
24 changes: 17 additions & 7 deletions operator/api/v1alpha1/keptnappversion_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ type KeptnAppVersionStatus struct {
// +kubebuilder:default:=Pending
PostDeploymentEvaluationStatus common.KeptnState `json:"postDeploymentEvaluationStatus,omitempty"`
// +kubebuilder:default:=Pending
WorkloadOverallStatus common.KeptnState `json:"workloadOverallStatus,omitempty"`
WorkloadStatus []WorkloadStatus `json:"workloadStatus,omitempty"`
CurrentPhase string `json:"currentPhase,omitempty"`
PreDeploymentTaskStatus []TaskStatus `json:"preDeploymentTaskStatus,omitempty"`
PostDeploymentTaskStatus []TaskStatus `json:"postDeploymentTaskStatus,omitempty"`
PreDeploymentEvaluationTaskStatus []EvaluationStatus `json:"preDeploymentEvaluationTaskStatus,omitempty"`
PostDeploymentEvaluationTaskStatus []EvaluationStatus `json:"postDeploymentEvaluationTaskStatus,omitempty"`
WorkloadOverallStatus common.KeptnState `json:"workloadOverallStatus,omitempty"`
WorkloadStatus []WorkloadStatus `json:"workloadStatus,omitempty"`
CurrentPhase string `json:"currentPhase,omitempty"`
PreDeploymentTaskStatus []TaskStatus `json:"preDeploymentTaskStatus,omitempty"`
PostDeploymentTaskStatus []TaskStatus `json:"postDeploymentTaskStatus,omitempty"`
PreDeploymentEvaluationTaskStatus []EvaluationStatus `json:"preDeploymentEvaluationTaskStatus,omitempty"`
PostDeploymentEvaluationTaskStatus []EvaluationStatus `json:"postDeploymentEvaluationTaskStatus,omitempty"`
PhaseTraceIDs common.PhaseTraceID `json:"phaseTraceIDs,omitempty"`
// +kubebuilder:default:=Pending
Status common.KeptnState `json:"status,omitempty"`

Expand Down Expand Up @@ -336,6 +337,9 @@ func (a KeptnAppVersion) GenerateEvaluation(traceContextCarrier propagation.MapC
}

func (a KeptnAppVersion) GetSpanName(phase string) string {
if phase == "" {
return a.Name
}
return phase
}

Expand Down Expand Up @@ -372,5 +376,11 @@ func (a *KeptnAppVersion) CancelRemainingPhases(phase common.KeptnPhaseType) {
a.Status.PostDeploymentStatus = common.StateCancelled
a.Status.PostDeploymentEvaluationStatus = common.StateCancelled
a.Status.Status = common.StateFailed
}

func (a *KeptnAppVersion) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) {
if a.Status.PhaseTraceIDs == nil {
a.Status.PhaseTraceIDs = common.PhaseTraceID{}
}
a.Status.PhaseTraceIDs[common.GetShortPhaseName(phase)] = carrier
}
27 changes: 19 additions & 8 deletions operator/api/v1alpha1/keptnworkloadinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ type KeptnWorkloadInstanceStatus struct {
// +kubebuilder:default:=Pending
PostDeploymentEvaluationStatus common.KeptnState `json:"postDeploymentEvaluationStatus,omitempty"`
// +kubebuilder:default:=Pending
PostDeploymentStatus common.KeptnState `json:"postDeploymentStatus,omitempty"`
PreDeploymentTaskStatus []TaskStatus `json:"preDeploymentTaskStatus,omitempty"`
PostDeploymentTaskStatus []TaskStatus `json:"postDeploymentTaskStatus,omitempty"`
PreDeploymentEvaluationTaskStatus []EvaluationStatus `json:"preDeploymentEvaluationTaskStatus,omitempty"`
PostDeploymentEvaluationTaskStatus []EvaluationStatus `json:"postDeploymentEvaluationTaskStatus,omitempty"`
StartTime metav1.Time `json:"startTime,omitempty"`
EndTime metav1.Time `json:"endTime,omitempty"`
CurrentPhase string `json:"currentPhase,omitempty"`
PostDeploymentStatus common.KeptnState `json:"postDeploymentStatus,omitempty"`
PreDeploymentTaskStatus []TaskStatus `json:"preDeploymentTaskStatus,omitempty"`
PostDeploymentTaskStatus []TaskStatus `json:"postDeploymentTaskStatus,omitempty"`
PreDeploymentEvaluationTaskStatus []EvaluationStatus `json:"preDeploymentEvaluationTaskStatus,omitempty"`
PostDeploymentEvaluationTaskStatus []EvaluationStatus `json:"postDeploymentEvaluationTaskStatus,omitempty"`
StartTime metav1.Time `json:"startTime,omitempty"`
EndTime metav1.Time `json:"endTime,omitempty"`
CurrentPhase string `json:"currentPhase,omitempty"`
PhaseTraceIDs common.PhaseTraceID `json:"phaseTraceIDs,omitempty"`
// +kubebuilder:default:=Pending
Status common.KeptnState `json:"status,omitempty"`
}
Expand Down Expand Up @@ -388,6 +389,9 @@ func (w KeptnWorkloadInstance) GetSpanKey(phase string) string {
}

func (w KeptnWorkloadInstance) GetSpanName(phase string) string {
if phase == "" {
return w.Name
}
return fmt.Sprintf("%s/%s", w.Spec.WorkloadName, phase)
}

Expand All @@ -411,3 +415,10 @@ func (w *KeptnWorkloadInstance) CancelRemainingPhases(phase common.KeptnPhaseTyp
w.Status.PostDeploymentEvaluationStatus = common.StateCancelled
w.Status.Status = common.StateFailed
}

func (w *KeptnWorkloadInstance) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) {
if w.Status.PhaseTraceIDs == nil {
w.Status.PhaseTraceIDs = common.PhaseTraceID{}
}
w.Status.PhaseTraceIDs[common.GetShortPhaseName(phase)] = carrier
}
38 changes: 38 additions & 0 deletions operator/api/v1alpha1/tests/keptnappversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/keptn/lifecycle-toolkit/operator/api/v1alpha1/common"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -313,6 +314,43 @@ func TestKeptnAppVersion_CancelRemainingPhases(t *testing.T) {
}
}

func TestKeptnAppVersion_SetPhaseTraceID(t *testing.T) {
app := v1alpha1.KeptnAppVersion{
Status: v1alpha1.KeptnAppVersionStatus{},
}

app.SetPhaseTraceID(common.PhaseAppDeployment.ShortName, propagation.MapCarrier{
"name3": "trace3",
})

require.Equal(t, v1alpha1.KeptnAppVersion{
Status: v1alpha1.KeptnAppVersionStatus{
PhaseTraceIDs: common.PhaseTraceID{
common.PhaseAppDeployment.ShortName: propagation.MapCarrier{
"name3": "trace3",
},
},
},
}, app)

app.SetPhaseTraceID(common.PhaseWorkloadDeployment.LongName, propagation.MapCarrier{
"name2": "trace2",
})

require.Equal(t, v1alpha1.KeptnAppVersion{
Status: v1alpha1.KeptnAppVersionStatus{
PhaseTraceIDs: common.PhaseTraceID{
common.PhaseAppDeployment.ShortName: propagation.MapCarrier{
"name3": "trace3",
},
common.PhaseWorkloadDeployment.ShortName: propagation.MapCarrier{
"name2": "trace2",
},
},
},
}, app)
}

func TestKeptnAppVersionList(t *testing.T) {
list := v1alpha1.KeptnAppVersionList{
Items: []v1alpha1.KeptnAppVersion{
Expand Down
40 changes: 40 additions & 0 deletions operator/api/v1alpha1/tests/keptnworkloadinstance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/keptn/lifecycle-toolkit/operator/api/v1alpha1/common"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -205,6 +206,8 @@ func TestKeptnWorkloadInstance(t *testing.T) {
},
}, evaluation.Spec)

require.Equal(t, "workload", workload.GetSpanName(""))

require.Equal(t, "workloadname/phase", workload.GetSpanName("phase"))

require.Equal(t, []attribute.KeyValue{
Expand Down Expand Up @@ -284,6 +287,43 @@ func TestKeptnWorkloadInstance_CancelRemainingPhases(t *testing.T) {
}
}

func TestKeptnWorkloadInstance_SetPhaseTraceID(t *testing.T) {
app := v1alpha1.KeptnWorkloadInstance{
Status: v1alpha1.KeptnWorkloadInstanceStatus{},
}

app.SetPhaseTraceID(common.PhaseAppDeployment.ShortName, propagation.MapCarrier{
"name3": "trace3",
})

require.Equal(t, v1alpha1.KeptnWorkloadInstance{
Status: v1alpha1.KeptnWorkloadInstanceStatus{
PhaseTraceIDs: common.PhaseTraceID{
common.PhaseAppDeployment.ShortName: propagation.MapCarrier{
"name3": "trace3",
},
},
},
}, app)

app.SetPhaseTraceID(common.PhaseWorkloadDeployment.LongName, propagation.MapCarrier{
"name2": "trace2",
})

require.Equal(t, v1alpha1.KeptnWorkloadInstance{
Status: v1alpha1.KeptnWorkloadInstanceStatus{
PhaseTraceIDs: common.PhaseTraceID{
common.PhaseAppDeployment.ShortName: propagation.MapCarrier{
"name3": "trace3",
},
common.PhaseWorkloadDeployment.ShortName: propagation.MapCarrier{
"name2": "trace2",
},
},
},
}, app)
}

func TestKeptnWorkloadInstanceList(t *testing.T) {
list := v1alpha1.KeptnWorkloadInstanceList{
Items: []v1alpha1.KeptnWorkloadInstance{
Expand Down
36 changes: 36 additions & 0 deletions operator/api/v1alpha1/zz_generated.deepcopy.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.9.2
controller-gen.kubebuilder.io/version: v0.10.0
creationTimestamp: null
name: keptnapps.lifecycle.keptn.sh
spec:
Expand Down
Loading