diff --git a/internal/command/consts.go b/internal/command/consts.go index 21eadc2..18fbb76 100644 --- a/internal/command/consts.go +++ b/internal/command/consts.go @@ -17,15 +17,16 @@ const ( ) var ( - scoreFile string - overridesFile string - extensionsFile string - uiUrl string - apiUrl string - apiToken string - orgID string - appID string - envID string + scoreFile string + overridesFile string + extensionsFile string + uiUrl string + apiUrl string + apiToken string + orgID string + appID string + envID string + workloadSourceURL string overrideParams []string message string diff --git a/internal/command/delta.go b/internal/command/delta.go index 799ec32..77045b9 100644 --- a/internal/command/delta.go +++ b/internal/command/delta.go @@ -25,6 +25,7 @@ func init() { deltaCmd.Flags().StringVarP(&scoreFile, "file", "f", scoreFileDefault, "Source SCORE file") deltaCmd.Flags().StringVar(&overridesFile, "overrides", overridesFileDefault, "Overrides file") deltaCmd.Flags().StringVar(&extensionsFile, "extensions", extensionsFileDefault, "Extensions file") + deltaCmd.Flags().StringVar(&workloadSourceURL, "workload-source-url", "", "URL of file that is managing the humanitec workload") deltaCmd.Flags().StringVar(&uiUrl, "ui-url", uiUrlDefault, "Humanitec UI") deltaCmd.Flags().StringVar(&apiUrl, "api-url", apiUrlDefault, "Humanitec API endpoint") deltaCmd.Flags().StringVar(&deltaID, "delta", "", "The ID of an existing delta in Humanitec into which to merge the generated delta") @@ -74,7 +75,7 @@ func delta(cmd *cobra.Command, args []string) error { // Prepare a new deployment // log.Print("Preparing a new deployment...\n") - delta, err := humanitec.ConvertSpec(message, envID, spec, ext) + delta, err := humanitec.ConvertSpec(message, envID, workloadSourceURL, spec, ext) if err != nil { return fmt.Errorf("preparing new deployment: %w", err) } diff --git a/internal/command/draft.go b/internal/command/draft.go index bd51057..bfd1c84 100644 --- a/internal/command/draft.go +++ b/internal/command/draft.go @@ -15,6 +15,7 @@ func init() { draftCmd.Flags().StringVarP(&scoreFile, "file", "f", scoreFileDefault, "Source SCORE file") draftCmd.Flags().StringVar(&overridesFile, "overrides", overridesFileDefault, "Overrides file") draftCmd.Flags().StringVar(&extensionsFile, "extensions", extensionsFileDefault, "Extensions file") + draftCmd.Flags().StringVar(&workloadSourceURL, "workload-source-url", "", "URL of file that is managing the humanitec workload") draftCmd.Flags().StringVar(&uiUrl, "ui-url", uiUrlDefault, "Humanitec API endpoint") draftCmd.Flags().StringVar(&apiUrl, "api-url", apiUrlDefault, "Humanitec API endpoint") draftCmd.Flags().StringVar(&apiToken, "token", "", "Humanitec API authentication token") diff --git a/internal/command/run.go b/internal/command/run.go index c8dc2c9..3f9fed9 100644 --- a/internal/command/run.go +++ b/internal/command/run.go @@ -35,6 +35,7 @@ func init() { runCmd.Flags().StringVarP(&scoreFile, "file", "f", scoreFileDefault, "Source SCORE file") runCmd.Flags().StringVar(&overridesFile, "overrides", overridesFileDefault, "Overrides file") runCmd.Flags().StringVar(&extensionsFile, "extensions", extensionsFileDefault, "Extensions file") + runCmd.Flags().StringVar(&workloadSourceURL, "workload-source-url", "", "URL of file that is managing the humanitec workload") runCmd.Flags().StringVar(&envID, "env", "", "Environment ID") runCmd.MarkFlagRequired("env") @@ -68,7 +69,7 @@ func run(cmd *cobra.Command, args []string) error { // Prepare a new deployment // log.Print("Preparing a new deployment...\n") - delta, err := humanitec.ConvertSpec(message, envID, spec, ext) + delta, err := humanitec.ConvertSpec(message, envID, workloadSourceURL, spec, ext) if err != nil { return fmt.Errorf("preparing new deployment: %w", err) } diff --git a/internal/humanitec/consts.go b/internal/humanitec/consts.go index 268a262..59ceb28 100644 --- a/internal/humanitec/consts.go +++ b/internal/humanitec/consts.go @@ -1,5 +1,8 @@ package humanitec const ( - DefaultWorkloadProfile = "humanitec/default-module" + DefaultWorkloadProfile = "humanitec/default-module" + managedBy = "score-humanitec" + managedByAnnotation = "humanitec.io/managed-by" + workloadSourceAnnotation = "humanitec.io/workload-source" ) diff --git a/internal/humanitec/convert.go b/internal/humanitec/convert.go index fde1d3c..adb144a 100644 --- a/internal/humanitec/convert.go +++ b/internal/humanitec/convert.go @@ -143,11 +143,17 @@ func convertContainerSpec(name string, spec *score.ContainerSpec, context *templ } // ConvertSpec converts SCORE specification into Humanitec deployment delta. -func ConvertSpec(name, envID string, spec *score.WorkloadSpec, ext *extensions.HumanitecExtensionsSpec) (*humanitec.CreateDeploymentDeltaRequest, error) { +func ConvertSpec(name, envID string, workloadSourceURL string, spec *score.WorkloadSpec, ext *extensions.HumanitecExtensionsSpec) (*humanitec.CreateDeploymentDeltaRequest, error) { ctx, err := buildContext(spec.Metadata, spec.Resources, ext.Resources) if err != nil { return nil, fmt.Errorf("preparing context: %w", err) } + annotations := map[string]interface{}{ + managedByAnnotation: managedBy, + } + if workloadSourceURL != "" { + annotations[workloadSourceAnnotation] = workloadSourceURL + } var containers = make(map[string]interface{}, len(spec.Containers)) for cName, cSpec := range spec.Containers { @@ -159,7 +165,8 @@ func ConvertSpec(name, envID string, spec *score.WorkloadSpec, ext *extensions.H } var workloadSpec = map[string]interface{}{ - "containers": containers, + "annotations": annotations, + "containers": containers, } if len(spec.Service.Ports) > 0 { var ports = map[string]interface{}{} diff --git a/internal/humanitec/convert_test.go b/internal/humanitec/convert_test.go index ea357d0..27ef162 100644 --- a/internal/humanitec/convert_test.go +++ b/internal/humanitec/convert_test.go @@ -104,11 +104,12 @@ func TestScoreConvert(t *testing.T) { ) var tests = []struct { - Name string - Source *score.WorkloadSpec - Extensions *extensions.HumanitecExtensionsSpec - Output *humanitec.CreateDeploymentDeltaRequest - Error error + Name string + Source *score.WorkloadSpec + Extensions *extensions.HumanitecExtensionsSpec + Output *humanitec.CreateDeploymentDeltaRequest + WorkloadSourceURL string + Error error }{ { Name: "Should convert SCORE to deployment delta", @@ -168,7 +169,8 @@ func TestScoreConvert(t *testing.T) { }, }, }, - Extensions: &extensions.HumanitecExtensionsSpec{}, + Extensions: &extensions.HumanitecExtensionsSpec{}, + WorkloadSourceURL: "", Output: &humanitec.CreateDeploymentDeltaRequest{ Metadata: humanitec.DeltaMetadata{EnvID: envID, Name: name}, Modules: humanitec.ModuleDeltas{ @@ -176,6 +178,9 @@ func TestScoreConvert(t *testing.T) { "backend": { "profile": "humanitec/default-module", "spec": map[string]interface{}{ + "annotations": map[string]interface{}{ + "humanitec.io/managed-by": "score-humanitec", + }, "containers": map[string]interface{}{ "backend": map[string]interface{}{ "id": "backend", @@ -352,6 +357,7 @@ func TestScoreConvert(t *testing.T) { }, }, }, + WorkloadSourceURL: "https://test.com", Output: &humanitec.CreateDeploymentDeltaRequest{ Metadata: humanitec.DeltaMetadata{EnvID: envID, Name: name}, Modules: humanitec.ModuleDeltas{ @@ -359,6 +365,10 @@ func TestScoreConvert(t *testing.T) { "test": { "profile": "test-org/test-module", "spec": map[string]interface{}{ + "annotations": map[string]interface{}{ + "humanitec.io/managed-by": "score-humanitec", + "humanitec.io/workload-source": "https://test.com", + }, "containers": map[string]interface{}{ "backend": map[string]interface{}{ "id": "backend", @@ -444,7 +454,7 @@ func TestScoreConvert(t *testing.T) { for _, tt := range tests { t.Run(tt.Name, func(t *testing.T) { - res, err := ConvertSpec(name, envID, tt.Source, tt.Extensions) + res, err := ConvertSpec(name, envID, tt.WorkloadSourceURL, tt.Source, tt.Extensions) if tt.Error != nil { // On Error