Skip to content

Commit

Permalink
inspect
Browse files Browse the repository at this point in the history
Signed-off-by: Gerd Oberlechner <[email protected]>
  • Loading branch information
geoberle committed Nov 20, 2024
1 parent 954986d commit 5863129
Show file tree
Hide file tree
Showing 14 changed files with 434 additions and 70 deletions.
2 changes: 1 addition & 1 deletion backend/pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ resourceGroups:
steps:
- name: deploy
action: Shell
command: ["/bin/bash", "-c", "make -f Makefile.deploy deploy"]
command: ["/bin/bash", "-c", "make deploy"]
env:
- name: ARO_HCP_IMAGE_ACR
configRef: svcAcrName
Expand Down
13 changes: 8 additions & 5 deletions setup-env.mk
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
SHELL = /bin/bash
SHELLFLAGS = -eu -o pipefail -c
SHELLFLAGS = -eu -o pipefail

ifndef EV2
ifndef RUNS_IN_TEMPLATIZE
PROJECT_ROOT_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))

DEPLOY_ENV ?= personal-dev
PIPELINE ?= pipeline.yaml
PIPELINE_STEP ?= deploy
HASH = $(shell echo -n "$(DEPLOY_ENV)$(PIPELINE)$(PIPELINE_STEP)" | md5)
ENV_VARS_FILE ?= /tmp/deploy.${HASH}.cfg
ENV_VARS_FILE ?= ${TMPDIR}/deploy.${HASH}.cfg

# Target to generate the environment variables file
$(ENV_VARS_FILE): ${PROJECT_ROOT_DIR}/config/config.yaml ${PIPELINE} ${PROJECT_ROOT_DIR}/templatize.sh ${MAKEFILE_LIST}
@echo "generate env vars"
@echo "generate env vars file ${ENV_VARS_FILE}"
@${PROJECT_ROOT_DIR}/templatize.sh ${DEPLOY_ENV} \
-p ${PIPELINE} \
-s ${PIPELINE_STEP} > $@
-s ${PIPELINE_STEP} > $(ENV_VARS_FILE)

# Include the environment variables file if it exists
ifndef EV2
-include ${ENV_VARS_FILE}
endif
endif
5 changes: 3 additions & 2 deletions templatize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ if [ -n "$INPUT" ] && [ -n "$OUTPUT" ]; then
--output=${OUTPUT} \
${EXTRA_ARGS}
elif [ -n "$PIPELINE" ] && [ -n "$PIPELINE_STEP" ]; then
$TEMPLATIZE run-pipeline \
$TEMPLATIZE pipeline inspect \
--config-file=${CONFIG_FILE} \
--cloud=${CLOUD} \
--deploy-env=${DEPLOY_ENV} \
Expand All @@ -137,7 +137,8 @@ elif [ -n "$PIPELINE" ] && [ -n "$PIPELINE_STEP" ]; then
--stamp=${CXSTAMP} \
--pipeline-file=${PIPELINE} \
--step=${PIPELINE_STEP} \
--dump-step-vars
--aspect vars \
--format Makefile
else
$TEMPLATIZE inspect \
--config-file=${CONFIG_FILE} \
Expand Down
19 changes: 19 additions & 0 deletions tooling/templatize/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

SHELL = /bin/bash

# Define the binary name
BINARY = templatize

# Define the source files
SOURCES = $(shell find . -name '*.go')

# Build the binary
$(BINARY): $(SOURCES) $(MAKEFILE_LIST)
@echo "rebuild templatize"
go build -o $(BINARY) .

# Clean the build artifacts
clean:
rm -f $(BINARY)

.PHONY: clean
24 changes: 24 additions & 0 deletions tooling/templatize/cmd/pipeline/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pipeline

import (
"github.com/Azure/ARO-HCP/tooling/templatize/cmd/pipeline/inspect"
"github.com/Azure/ARO-HCP/tooling/templatize/cmd/pipeline/run"
"github.com/spf13/cobra"
)

func NewCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "pipeline",
Short: "pipeline",
Long: "pipeline",
SilenceUsage: true,
TraverseChildren: true,
CompletionOptions: cobra.CompletionOptions{
HiddenDefaultCmd: true,
},
}
cmd.AddCommand(run.NewCommand())
cmd.AddCommand(inspect.NewCommand())

return cmd
}
42 changes: 42 additions & 0 deletions tooling/templatize/cmd/pipeline/inspect/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package inspect

import (
"context"
"log"
"os"
"os/signal"
"syscall"

"github.com/spf13/cobra"
)

func NewCommand() *cobra.Command {
opts := DefaultOptions()
cmd := &cobra.Command{
Use: "inspect",
Short: "inspect aspects of a pipeline.yaml file",
Long: "inspect aspects of a pipeline.yaml file",
RunE: func(cmd *cobra.Command, args []string) error {
ctx, stop := signal.NotifyContext(cmd.Context(), os.Interrupt, syscall.SIGTERM)
defer stop()

return runInspect(ctx, opts)
},
}
if err := BindOptions(opts, cmd); err != nil {
log.Fatal(err)
}
return cmd
}

func runInspect(ctx context.Context, opts *RawInspectOptions) error {
validated, err := opts.Validate()
if err != nil {
return err
}
completed, err := validated.Complete()
if err != nil {
return err
}
return completed.RunInspect(ctx)
}
109 changes: 109 additions & 0 deletions tooling/templatize/cmd/pipeline/inspect/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package inspect

import (
"context"
"fmt"

"github.com/Azure/ARO-HCP/tooling/templatize/cmd/pipeline/options"
"github.com/Azure/ARO-HCP/tooling/templatize/pkg/config"
"github.com/Azure/ARO-HCP/tooling/templatize/pkg/pipeline"
"github.com/spf13/cobra"
)

func DefaultOptions() *RawInspectOptions {
return &RawInspectOptions{
PipelineOptions: options.DefaultOptions(),
}
}

func BindOptions(opts *RawInspectOptions, cmd *cobra.Command) error {
err := options.BindOptions(opts.PipelineOptions, cmd)
if err != nil {
return fmt.Errorf("failed to bind options: %w", err)
}
cmd.Flags().StringVar(&opts.Aspect, "aspect", opts.Aspect, "aspect of the pipeline to inspect")
cmd.Flags().StringVar(&opts.Format, "format", opts.Format, "output format")
return nil
}

type RawInspectOptions struct {
PipelineOptions *options.RawPipelineOptions
Aspect string
Format string
}

// validatedInspectOptions is a private wrapper that enforces a call of Validate() before Complete() can be invoked.
type validatedInspectOptions struct {
*RawInspectOptions
*options.ValidatedPipelineOptions
}

type ValidatedInspectOptions struct {
// Embed a private pointer that cannot be instantiated outside of this package.
*validatedInspectOptions
}

// completedRunOptions is a private wrapper that enforces a call of Complete() before config generation can be invoked.
type completedInspectOptions struct {
PipelineOptions *options.PipelineOptions
Aspect string
Format string
}

type InspectOptions struct {
// Embed a private pointer that cannot be instantiated outside of this package.
*completedInspectOptions
}

func (o *RawInspectOptions) Validate() (*ValidatedInspectOptions, error) {
validatedPipelineOptions, err := o.PipelineOptions.Validate()
if err != nil {
return nil, err
}
// todo validate aspect
return &ValidatedInspectOptions{
validatedInspectOptions: &validatedInspectOptions{
RawInspectOptions: o,
ValidatedPipelineOptions: validatedPipelineOptions,
},
}, nil
}

func (o *ValidatedInspectOptions) Complete() (*InspectOptions, error) {
completed, err := o.ValidatedPipelineOptions.Complete()
if err != nil {
return nil, err
}

return &InspectOptions{
completedInspectOptions: &completedInspectOptions{
PipelineOptions: completed,
Aspect: o.Aspect,
Format: o.Format,
},
}, nil
}

func (o *InspectOptions) RunInspect(ctx context.Context) error {
rolloutOptions := o.PipelineOptions.RolloutOptions
variables, err := rolloutOptions.Options.ConfigProvider.GetVariables(
rolloutOptions.Cloud,
rolloutOptions.DeployEnv,
rolloutOptions.Region,
config.NewConfigReplacements(
rolloutOptions.Region,
rolloutOptions.RegionShort,
rolloutOptions.Stamp,
),
)
if err != nil {
return err
}
return o.PipelineOptions.Pipeline.Inspect(ctx, &pipeline.PipelineInspectOptions{
Vars: variables,
Region: rolloutOptions.Region,
Step: o.PipelineOptions.Step,
Aspect: o.Aspect,
Format: o.Format,
})
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
package run
package options

import (
"context"
"fmt"
"os"

"github.com/spf13/cobra"

options "github.com/Azure/ARO-HCP/tooling/templatize/cmd"
"github.com/Azure/ARO-HCP/tooling/templatize/pkg/config"
"github.com/Azure/ARO-HCP/tooling/templatize/pkg/pipeline"
)

func DefaultOptions() *RawRunOptions {
return &RawRunOptions{
func DefaultOptions() *RawPipelineOptions {
return &RawPipelineOptions{
RolloutOptions: options.DefaultRolloutOptions(),
}
}

func BindOptions(opts *RawRunOptions, cmd *cobra.Command) error {
func BindOptions(opts *RawPipelineOptions, cmd *cobra.Command) error {
err := options.BindRolloutOptions(opts.RolloutOptions, cmd)
if err != nil {
return fmt.Errorf("failed to bind options: %w", err)
}
cmd.Flags().StringVar(&opts.PipelineFile, "pipeline-file", opts.PipelineFile, "pipeline file path")
cmd.Flags().BoolVar(&opts.DryRun, "dry-run", opts.DryRun, "validate the pipeline without executing it")
cmd.Flags().StringVar(&opts.Step, "step", opts.Step, "run only a specific step in the pipeline")

for _, flag := range []string{"pipeline-file"} {
if err := cmd.MarkFlagFilename(flag); err != nil {
Expand All @@ -38,36 +36,36 @@ func BindOptions(opts *RawRunOptions, cmd *cobra.Command) error {
}

// RawRunOptions holds input values.
type RawRunOptions struct {
type RawPipelineOptions struct {
RolloutOptions *options.RawRolloutOptions
PipelineFile string
DryRun bool
Step string
}

// validatedRunOptions is a private wrapper that enforces a call of Validate() before Complete() can be invoked.
type validatedRunOptions struct {
*RawRunOptions
// validatedPipelineOptions is a private wrapper that enforces a call of Validate() before Complete() can be invoked.
type validatedPipelineOptions struct {
*RawPipelineOptions
*options.ValidatedRolloutOptions
}

type ValidatedRunOptions struct {
type ValidatedPipelineOptions struct {
// Embed a private pointer that cannot be instantiated outside of this package.
*validatedRunOptions
*validatedPipelineOptions
}

// completedRunOptions is a private wrapper that enforces a call of Complete() before config generation can be invoked.
type completedRunOptions struct {
// completedPipelineOptions is a private wrapper that enforces a call of Complete() before config generation can be invoked.
type completedPipelineOptions struct {
RolloutOptions *options.RolloutOptions
Pipeline *pipeline.Pipeline
DryRun bool
Step string
}

type RunOptions struct {
type PipelineOptions struct {
// Embed a private pointer that cannot be instantiated outside of this package.
*completedRunOptions
*completedPipelineOptions
}

func (o *RawRunOptions) Validate() (*ValidatedRunOptions, error) {
func (o *RawPipelineOptions) Validate() (*ValidatedPipelineOptions, error) {
validatedRolloutOptions, err := o.RolloutOptions.Validate()
if err != nil {
return nil, err
Expand All @@ -77,15 +75,15 @@ func (o *RawRunOptions) Validate() (*ValidatedRunOptions, error) {
return nil, fmt.Errorf("pipeline file %s does not exist", o.PipelineFile)
}

return &ValidatedRunOptions{
validatedRunOptions: &validatedRunOptions{
RawRunOptions: o,
return &ValidatedPipelineOptions{
validatedPipelineOptions: &validatedPipelineOptions{
RawPipelineOptions: o,
ValidatedRolloutOptions: validatedRolloutOptions,
},
}, nil
}

func (o *ValidatedRunOptions) Complete() (*RunOptions, error) {
func (o *ValidatedPipelineOptions) Complete() (*PipelineOptions, error) {
completed, err := o.ValidatedRolloutOptions.Complete()
if err != nil {
return nil, err
Expand All @@ -96,32 +94,11 @@ func (o *ValidatedRunOptions) Complete() (*RunOptions, error) {
return nil, fmt.Errorf("failed to load pipeline file %s: %w", o.PipelineFile, err)
}

return &RunOptions{
completedRunOptions: &completedRunOptions{
return &PipelineOptions{
completedPipelineOptions: &completedPipelineOptions{
RolloutOptions: completed,
Pipeline: pipeline,
DryRun: o.DryRun,
Step: o.Step,
},
}, nil
}

func (o *RunOptions) RunPipeline(ctx context.Context) error {
variables, err := o.RolloutOptions.Options.ConfigProvider.GetVariables(
o.RolloutOptions.Cloud,
o.RolloutOptions.DeployEnv,
o.RolloutOptions.Region,
config.NewConfigReplacements(
o.RolloutOptions.Region,
o.RolloutOptions.RegionShort,
o.RolloutOptions.Stamp,
),
)
if err != nil {
return err
}
return o.Pipeline.Run(ctx, &pipeline.PipelineRunOptions{
DryRun: o.DryRun,
Vars: variables,
Region: o.RolloutOptions.Region,
})
}
Loading

0 comments on commit 5863129

Please sign in to comment.