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: Stacks improvements #311

Merged
merged 1 commit into from
Nov 6, 2024
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ require (
github.com/open-policy-agent/gatekeeper/v3 v3.17.1
github.com/orcaman/concurrent-map/v2 v2.0.1
github.com/pkg/errors v0.9.1
github.com/pluralsh/console/go/client v1.22.3
github.com/pluralsh/console/go/client v1.22.5
github.com/pluralsh/controller-reconcile-helper v0.1.0
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34
github.com/pluralsh/polly v0.1.10
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -666,8 +666,8 @@ github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5/go.mod h1:iIss55rK
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pluralsh/console/go/client v1.22.3 h1:5CUV4E/EH5G84ZVIIdr4NuUzM92AKUrZBLEh2SjLeEc=
github.com/pluralsh/console/go/client v1.22.3/go.mod h1:lpoWASYsM9keNePS3dpFiEisUHEfObIVlSL3tzpKn8k=
github.com/pluralsh/console/go/client v1.22.5 h1:kOc2uP0Nrgh5IyiqfQTEAKQr4aXEUEYr697FLmeAaN0=
github.com/pluralsh/console/go/client v1.22.5/go.mod h1:lpoWASYsM9keNePS3dpFiEisUHEfObIVlSL3tzpKn8k=
github.com/pluralsh/controller-reconcile-helper v0.1.0 h1:BV3dYZFH5rn8ZvZjtpkACSv/GmLEtRftNQj/Y4ddHEo=
github.com/pluralsh/controller-reconcile-helper v0.1.0/go.mod h1:RxAbvSB4/jkvx616krCdNQXPbpGJXW3J1L3rASxeFOA=
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34 h1:ab2PN+6if/Aq3/sJM0AVdy1SYuMAnq4g20VaKhTm/Bw=
Expand Down
10 changes: 5 additions & 5 deletions internal/controller/stackrunjob_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ var _ = Describe("Stack Run Job Controller", Ordered, func() {
runId := strings.TrimPrefix(completedName, "stack-")

fakeConsoleClient := mocks.NewClientMock(mocks.TestingT)
fakeConsoleClient.On("GetStackRun", mock.Anything).Return(&console.StackRunFragment{
fakeConsoleClient.On("GetStackRun", mock.Anything).Return(&console.StackRunMinimalFragment{
ID: runId,
Status: console.StackStatusSuccessful,
}, nil)
fakeConsoleClient.On("UpdateStackRun", runId, mock.Anything).Return(&console.StackRunFragment{}, nil)
fakeConsoleClient.On("UpdateStackRun", runId, mock.Anything).Return(&console.StackRunMinimalFragment{}, nil)

reconciler := &StackRunJobReconciler{
Client: kClient,
Expand All @@ -129,7 +129,7 @@ var _ = Describe("Stack Run Job Controller", Ordered, func() {
runId := strings.TrimPrefix(completedName, "stack-")

fakeConsoleClient := mocks.NewClientMock(mocks.TestingT)
fakeConsoleClient.On("GetStackRun", mock.Anything).Return(&console.StackRunFragment{
fakeConsoleClient.On("GetStackRun", mock.Anything).Return(&console.StackRunMinimalFragment{
ID: runId,
Status: console.StackStatusSuccessful,
}, nil)
Expand All @@ -145,7 +145,7 @@ var _ = Describe("Stack Run Job Controller", Ordered, func() {

It("should exit without errors as stack run status was already updated", func() {
fakeConsoleClient := mocks.NewClientMock(mocks.TestingT)
fakeConsoleClient.On("GetStackRun", mock.Anything).Return(&console.StackRunFragment{
fakeConsoleClient.On("GetStackRun", mock.Anything).Return(&console.StackRunMinimalFragment{
ID: "2",
Status: console.StackStatusFailed,
}, nil)
Expand All @@ -163,7 +163,7 @@ var _ = Describe("Stack Run Job Controller", Ordered, func() {
runId := strings.TrimPrefix(runningName, "stack-")

fakeConsoleClient := mocks.NewClientMock(mocks.TestingT)
fakeConsoleClient.On("GetStackRun", mock.Anything).Return(&console.StackRunFragment{
fakeConsoleClient.On("GetStackRun", mock.Anything).Return(&console.StackRunMinimalFragment{
ID: runId,
Status: console.StackStatusRunning,
}, nil)
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ type Client interface {
GetNamespace(id string) (*console.ManagedNamespaceFragment, error)
ListNamespaces(after *string, first *int64) (*console.ListClusterNamespaces_ClusterManagedNamespaces, error)
GetStackRunBase(id string) (*v1.StackRun, error)
GetStackRun(id string) (*console.StackRunFragment, error)
GetStackRun(id string) (*console.StackRunMinimalFragment, error)
AddStackRunLogs(id, logs string) error
CompleteStackRun(id string, attributes console.StackRunAttributes) error
UpdateStackRun(id string, attributes console.StackRunAttributes) error
UpdateStackRunStep(id string, attributes console.RunStepAttributes) error
ListClusterStackRuns(after *string, first *int64) (*console.ListClusterStacks_ClusterStackRuns, error)
ListClusterStackRuns(after *string, first *int64) (*console.ListClusterStackIds_ClusterStackRuns, error)
GetUser(email string) (*console.UserFragment, error)
GetGroup(name string) (*console.GroupFragment, error)
SaveUpgradeInsights(attributes []*console.UpgradeInsightAttributes) (*console.SaveUpgradeInsights, error)
Expand Down
10 changes: 5 additions & 5 deletions pkg/client/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,22 @@ func (c *client) UpdateStackRunStep(id string, attributes gqlclient.RunStepAttri
return nil
}

func (c *client) GetStackRun(id string) (*gqlclient.StackRunFragment, error) {
restore, err := c.consoleClient.GetStackRun(c.ctx, id)
func (c *client) GetStackRun(id string) (*gqlclient.StackRunMinimalFragment, error) {
restore, err := c.consoleClient.GetStackRunMinimal(c.ctx, id)
if err != nil {
return nil, err
}

return restore.StackRun, nil
}

func (c *client) ListClusterStackRuns(after *string, first *int64) (*gqlclient.ListClusterStacks_ClusterStackRuns, error) {
resp, err := c.consoleClient.ListClusterStacks(c.ctx, after, first, nil, nil)
func (c *client) ListClusterStackRuns(after *string, first *int64) (*gqlclient.ListClusterStackIds_ClusterStackRuns, error) {
resp, err := c.consoleClient.ListClusterStackIds(c.ctx, after, first, nil, nil)
if err != nil {
return nil, err
}
if resp.ClusterStackRuns == nil {
return nil, fmt.Errorf("the response from ListInfrastructureStacks is nil")
return nil, fmt.Errorf("the response from ListClusterStackIds is nil")
}
return resp.ClusterStackRuns, nil
}
28 changes: 14 additions & 14 deletions pkg/controller/stacks/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func init() {
}
}

func (r *StackReconciler) reconcileRunJob(ctx context.Context, run *console.StackRunFragment) (*batchv1.Job, error) {
func (r *StackReconciler) reconcileRunJob(ctx context.Context, run *console.StackRunMinimalFragment) (*batchv1.Job, error) {
logger := log.FromContext(ctx)

name := GetRunResourceName(run)
Expand Down Expand Up @@ -129,7 +129,7 @@ func (r *StackReconciler) reconcileRunJob(ctx context.Context, run *console.Stac
}

// GetRunResourceName returns a resource name used for a job and a secret connected to a given run.
func GetRunResourceName(run *console.StackRunFragment) string {
func GetRunResourceName(run *console.StackRunMinimalFragment) string {
return fmt.Sprintf("stack-%s", run.ID)
}

Expand All @@ -146,7 +146,7 @@ func (r *StackReconciler) GetRunResourceNamespace(jobSpec *batchv1.JobSpec) (nam
return
}

func (r *StackReconciler) GenerateRunJob(run *console.StackRunFragment, jobSpec *batchv1.JobSpec, name, namespace string) (*batchv1.Job, error) {
func (r *StackReconciler) GenerateRunJob(run *console.StackRunMinimalFragment, jobSpec *batchv1.JobSpec, name, namespace string) (*batchv1.Job, error) {
var err error
// If user-defined job spec was not available initialize it here.
if jobSpec == nil {
Expand Down Expand Up @@ -223,7 +223,7 @@ func getRunJobSpec(name string, jobSpecFragment *console.JobSpecFragment) *batch
return jobSpec
}

func (r *StackReconciler) ensureDefaultContainer(containers []corev1.Container, run *console.StackRunFragment) []corev1.Container {
func (r *StackReconciler) ensureDefaultContainer(containers []corev1.Container, run *console.StackRunMinimalFragment) []corev1.Container {
if index := algorithms.Index(containers, func(container corev1.Container) bool {
return container.Name == DefaultJobContainer
}); index == -1 {
Expand All @@ -240,7 +240,7 @@ func (r *StackReconciler) ensureDefaultContainer(containers []corev1.Container,
return containers
}

func (r *StackReconciler) getDefaultContainer(run *console.StackRunFragment) corev1.Container {
func (r *StackReconciler) getDefaultContainer(run *console.StackRunMinimalFragment) corev1.Container {
return corev1.Container{
Name: DefaultJobContainer,
Image: r.getDefaultContainerImage(run),
Expand All @@ -254,7 +254,7 @@ func (r *StackReconciler) getDefaultContainer(run *console.StackRunFragment) cor
}
}

func (r *StackReconciler) getDefaultContainerImage(run *console.StackRunFragment) string {
func (r *StackReconciler) getDefaultContainerImage(run *console.StackRunMinimalFragment) string {
// In case image is not provided, it will use our default image.
// Image name format: <defaultImage>:<tag>
// Note: User has to make sure that the tag is correct and matches our naming scheme.
Expand Down Expand Up @@ -284,43 +284,43 @@ func (r *StackReconciler) getDefaultContainerImage(run *console.StackRunFragment
return fmt.Sprintf("%s:%s-%s-%s", r.getImage(run), r.getTag(run), strings.ToLower(string(run.Type)), r.getVersion(run))
}

func (r *StackReconciler) hasCustomImage(run *console.StackRunFragment) bool {
func (r *StackReconciler) hasCustomImage(run *console.StackRunMinimalFragment) bool {
return run.Configuration.Image != nil && len(*run.Configuration.Image) > 0
}

func (r *StackReconciler) getImage(run *console.StackRunFragment) string {
func (r *StackReconciler) getImage(run *console.StackRunMinimalFragment) string {
if r.hasCustomImage(run) {
return *run.Configuration.Image
}

return defaultContainerImages[run.Type]
}

func (r *StackReconciler) hasCustomVersion(run *console.StackRunFragment) bool {
func (r *StackReconciler) hasCustomVersion(run *console.StackRunMinimalFragment) bool {
return run.Configuration.Version != nil && len(*run.Configuration.Version) > 0
}

func (r *StackReconciler) getVersion(run *console.StackRunFragment) string {
func (r *StackReconciler) getVersion(run *console.StackRunMinimalFragment) string {
if r.hasCustomVersion(run) {
return *run.Configuration.Version
}

return defaultContainerVersions[run.Type]
}

func (r *StackReconciler) hasCustomTag(run *console.StackRunFragment) bool {
func (r *StackReconciler) hasCustomTag(run *console.StackRunMinimalFragment) bool {
return run.Configuration.Tag != nil && len(*run.Configuration.Tag) > 0
}

func (r *StackReconciler) getTag(run *console.StackRunFragment) string {
func (r *StackReconciler) getTag(run *console.StackRunMinimalFragment) string {
if r.hasCustomTag(run) {
return *run.Configuration.Tag
}

return defaultImageTag
}

func (r *StackReconciler) getDefaultContainerEnvFrom(run *console.StackRunFragment) []corev1.EnvFromSource {
func (r *StackReconciler) getDefaultContainerEnvFrom(run *console.StackRunMinimalFragment) []corev1.EnvFromSource {
return []corev1.EnvFromSource{
{
SecretRef: &corev1.SecretEnvSource{
Expand Down Expand Up @@ -390,7 +390,7 @@ func ensureDefaultContainerSecurityContext(sc *corev1.SecurityContext) *corev1.S
}
}

func (r *StackReconciler) ensureDefaultContainerResourcesRequests(containers []corev1.Container, run *console.StackRunFragment) ([]corev1.Container, error) {
func (r *StackReconciler) ensureDefaultContainerResourcesRequests(containers []corev1.Container, run *console.StackRunMinimalFragment) ([]corev1.Container, error) {
if run.JobSpec == nil || run.JobSpec.Requests == nil {
return containers, nil
}
Expand Down
28 changes: 14 additions & 14 deletions pkg/controller/stacks/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ func TestGetDefaultContainerImage(t *testing.T) {
reconciler := NewStackReconciler(fakeConsoleClient, kClient, scheme.Scheme, time.Minute, 0, namespace, "", "")
cases := []struct {
name string
run *console.StackRunFragment
run *console.StackRunMinimalFragment
expectedImage string
}{
{
name: "use_defaults_when_no_configuration_provided",
run: &console.StackRunFragment{
run: &console.StackRunMinimalFragment{
Type: console.StackTypeTerraform,
Configuration: console.StackConfigurationFragment{},
},
expectedImage: "ghcr.io/pluralsh/harness:0.4.29-terraform-1.8.2",
},
{
name: "custom_tool_version_provided",
run: &console.StackRunFragment{
run: &console.StackRunMinimalFragment{
Type: console.StackTypeTerraform,
Configuration: console.StackConfigurationFragment{
Version: lo.ToPtr("1.8.4"),
Expand All @@ -51,7 +51,7 @@ func TestGetDefaultContainerImage(t *testing.T) {
},
{
name: "custom_tag_provided",
run: &console.StackRunFragment{
run: &console.StackRunMinimalFragment{
Type: console.StackTypeTerraform,
Configuration: console.StackConfigurationFragment{
Tag: lo.ToPtr("0.4.99"),
Expand All @@ -61,7 +61,7 @@ func TestGetDefaultContainerImage(t *testing.T) {
},
{
name: "custom_image_and_tag_provided",
run: &console.StackRunFragment{
run: &console.StackRunMinimalFragment{
Type: console.StackTypeTerraform,
Configuration: console.StackConfigurationFragment{
Image: lo.ToPtr("ghcr.io/pluralsh/custom"),
Expand All @@ -72,7 +72,7 @@ func TestGetDefaultContainerImage(t *testing.T) {
},
{
name: "custom_image_provided",
run: &console.StackRunFragment{
run: &console.StackRunMinimalFragment{
Type: console.StackTypeTerraform,
Configuration: console.StackConfigurationFragment{
Image: lo.ToPtr("ghcr.io/pluralsh/custom"),
Expand All @@ -82,7 +82,7 @@ func TestGetDefaultContainerImage(t *testing.T) {
},
{
name: "custom_image_and_version_provided",
run: &console.StackRunFragment{
run: &console.StackRunMinimalFragment{
Type: console.StackTypeTerraform,
Configuration: console.StackConfigurationFragment{
Image: lo.ToPtr("ghcr.io/pluralsh/custom"),
Expand All @@ -93,7 +93,7 @@ func TestGetDefaultContainerImage(t *testing.T) {
},
{
name: "ignore_version_when_custom_tag_provided",
run: &console.StackRunFragment{
run: &console.StackRunMinimalFragment{
Type: console.StackTypeTerraform,
Configuration: console.StackConfigurationFragment{
Tag: lo.ToPtr("1.8.4"),
Expand All @@ -120,12 +120,12 @@ func TestGenerateRunJob(t *testing.T) {
reconciler := NewStackReconciler(fakeConsoleClient, kClient, scheme.Scheme, time.Minute, 0, namespace, "", "")
cases := []struct {
name string
run *console.StackRunFragment
run *console.StackRunMinimalFragment
expectedJobSpec batchv1.JobSpec
}{
{
name: "use_empty_job_spec",
run: &console.StackRunFragment{
run: &console.StackRunMinimalFragment{
ID: runID,
Type: console.StackTypeTerraform,
Configuration: console.StackConfigurationFragment{},
Expand All @@ -138,7 +138,7 @@ func TestGenerateRunJob(t *testing.T) {
},
{
name: "use_defaults",
run: &console.StackRunFragment{
run: &console.StackRunMinimalFragment{
ID: runID,
Type: console.StackTypeTerraform,
Configuration: console.StackConfigurationFragment{},
Expand All @@ -150,7 +150,7 @@ func TestGenerateRunJob(t *testing.T) {
},
{
name: "add_labels",
run: &console.StackRunFragment{
run: &console.StackRunMinimalFragment{
ID: runID,
Type: console.StackTypeTerraform,
Configuration: console.StackConfigurationFragment{},
Expand All @@ -171,7 +171,7 @@ func TestGenerateRunJob(t *testing.T) {
},
{
name: "add_sa",
run: &console.StackRunFragment{
run: &console.StackRunMinimalFragment{
ID: runID,
Type: console.StackTypeTerraform,
Configuration: console.StackConfigurationFragment{},
Expand All @@ -188,7 +188,7 @@ func TestGenerateRunJob(t *testing.T) {
},
{
name: "add_resources",
run: &console.StackRunFragment{
run: &console.StackRunMinimalFragment{
ID: runID,
Type: console.StackTypeTerraform,
Configuration: console.StackConfigurationFragment{},
Expand Down
10 changes: 5 additions & 5 deletions pkg/controller/stacks/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type StackReconciler struct {
k8sClient ctrlclient.Client
scheme *runtime.Scheme
stackQueue workqueue.TypedRateLimitingInterface[string]
stackCache *client.Cache[console.StackRunFragment]
stackCache *client.Cache[console.StackRunMinimalFragment]
namespace string
consoleURL string
deployToken string
Expand All @@ -41,7 +41,7 @@ func NewStackReconciler(consoleClient client.Client, k8sClient ctrlclient.Client
k8sClient: k8sClient,
scheme: scheme,
stackQueue: workqueue.NewTypedRateLimitingQueue(workqueue.DefaultTypedControllerRateLimiter[string]()),
stackCache: client.NewCache[console.StackRunFragment](refresh, func(id string) (*console.StackRunFragment, error) {
stackCache: client.NewCache[console.StackRunMinimalFragment](refresh, func(id string) (*console.StackRunMinimalFragment, error) {
return consoleClient.GetStackRun(id)
}),
consoleURL: consoleURL,
Expand Down Expand Up @@ -88,10 +88,10 @@ func (r *StackReconciler) ShutdownQueue() {
r.stackQueue.ShutDown()
}

func (r *StackReconciler) ListStacks(ctx context.Context) *algorithms.Pager[*console.StackRunEdgeFragment] {
func (r *StackReconciler) ListStacks(ctx context.Context) *algorithms.Pager[*console.StackRunIDEdgeFragment] {
logger := log.FromContext(ctx)
logger.Info("create stack run pager")
fetch := func(page *string, size int64) ([]*console.StackRunEdgeFragment, *algorithms.PageInfo, error) {
fetch := func(page *string, size int64) ([]*console.StackRunIDEdgeFragment, *algorithms.PageInfo, error) {
resp, err := r.consoleClient.ListClusterStackRuns(page, &size)
if err != nil {
logger.Error(err, "failed to fetch stack run")
Expand All @@ -104,7 +104,7 @@ func (r *StackReconciler) ListStacks(ctx context.Context) *algorithms.Pager[*con
}
return resp.Edges, pageInfo, nil
}
return algorithms.NewPager[*console.StackRunEdgeFragment](common.DefaultPageSize, fetch)
return algorithms.NewPager[*console.StackRunIDEdgeFragment](common.DefaultPageSize, fetch)
}

func (r *StackReconciler) Poll(ctx context.Context) error {
Expand Down
Loading
Loading