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

fix: inconsistent field names #42

Merged
merged 2 commits into from
Jun 18, 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
10 changes: 5 additions & 5 deletions api/v1beta1/context_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,25 @@ type Hooks struct {
BeforePlan []string `json:"beforePlan,omitempty"`
}

// +kubebuilder:validation:XValidation:message=only one of stack or stackId or moduleId should be set,rule=has(self.stack) != has(self.stackId) != has(self.moduleId)
// +kubebuilder:validation:XValidation:message=only one of stackName or stackId or moduleId should be set,rule=has(self.stackName) != has(self.stackId) != has(self.moduleId)
type Attachment struct {
// +kubebuilder:validation:MinLength=1
ModuleId *string `json:"moduleId,omitempty"`
// +kubebuilder:validation:MinLength=1
StackId *string `json:"stackId,omitempty"`
// +kubebuilder:validation:MinLength=1
Stack *string `json:"stack,omitempty"`
Priority *int `json:"priority,omitempty"`
StackName *string `json:"stackName,omitempty"`
Priority *int `json:"priority,omitempty"`
}

// ContextSpec defines the desired state of Context
// +kubebuilder:validation:XValidation:message=only one of space or spaceId should be set,rule=has(self.spaceId) != has(self.space)
// +kubebuilder:validation:XValidation:message=only one of spaceName or spaceId should be set,rule=has(self.spaceId) != has(self.spaceName)
type ContextSpec struct {
Name *string `json:"name,omitempty"`
// +kubebuilder:validation:MinLength=1
SpaceId *string `json:"spaceId,omitempty"`
// +kubebuilder:validation:MinLength=1
Space *string `json:"space,omitempty"`
SpaceName *string `json:"spaceName,omitempty"`
Description *string `json:"description,omitempty"`
Labels []string `json:"labels,omitempty"`

Expand Down
2 changes: 1 addition & 1 deletion api/v1beta1/policy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type PolicySpec struct {
// SpaceId is ID (slug) of the space the policy is in
SpaceId *string `json:"spaceId,omitempty"`

AttachedStacksNames []string `json:"attachedStacks,omitempty"`
AttachedStacksNames []string `json:"attachedStacksNames,omitempty"`
AttachedStacksIds []string `json:"attachedStacksIds,omitempty"`
}

Expand Down
8 changes: 4 additions & 4 deletions api/v1beta1/zz_generated.deepcopy.go

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

17 changes: 9 additions & 8 deletions config/crd/bases/app.spacelift.io_contexts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,17 @@ spec:
type: string
priority:
type: integer
stack:
stackId:
minLength: 1
type: string
stackId:
stackName:
minLength: 1
type: string
type: object
x-kubernetes-validations:
- message: only one of stack or stackId or moduleId should be set
rule: has(self.stack) != has(self.stackId) != has(self.moduleId)
- message: only one of stackName or stackId or moduleId should be
set
rule: has(self.stackName) != has(self.stackId) != has(self.moduleId)
type: array
description:
type: string
Expand Down Expand Up @@ -185,16 +186,16 @@ spec:
type: array
name:
type: string
space:
spaceId:
minLength: 1
type: string
spaceId:
spaceName:
minLength: 1
type: string
type: object
x-kubernetes-validations:
- message: only one of space or spaceId should be set
rule: has(self.spaceId) != has(self.space)
- message: only one of spaceName or spaceId should be set
rule: has(self.spaceId) != has(self.spaceName)
status:
description: ContextStatus defines the observed state of Context
properties:
Expand Down
4 changes: 2 additions & 2 deletions config/crd/bases/app.spacelift.io_policies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ spec:
spec:
description: PolicySpec defines the desired state of Policy
properties:
attachedStacks:
attachedStacksIds:
items:
type: string
type: array
attachedStacksIds:
attachedStacksNames:
items:
type: string
type: array
Expand Down
12 changes: 6 additions & 6 deletions internal/controller/context_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ func (r *ContextReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
log.IntoContext(ctx, logger)

// A context should always be linked to a valid space
if context.Spec.Space != nil {
logger := logger.WithValues(logging.SpaceName, *context.Spec.Space)
space, err := r.SpaceRepository.Get(ctx, types.NamespacedName{Namespace: context.Namespace, Name: *context.Spec.Space})
if context.Spec.SpaceName != nil {
logger := logger.WithValues(logging.SpaceName, *context.Spec.SpaceName)
space, err := r.SpaceRepository.Get(ctx, types.NamespacedName{Namespace: context.Namespace, Name: *context.Spec.SpaceName})
if err != nil {
if k8sErrors.IsNotFound(err) {
logger.V(logging.Level4).Info("Unable to find space for context, will retry in 10 seconds")
Expand Down Expand Up @@ -107,12 +107,12 @@ func (r *ContextReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

// For all stack attachment, ensure that all stacks are ready
for i, attachment := range context.Spec.Attachments {
if attachment.Stack != nil {
logger := logger.WithValues(logging.StackName, *attachment.Stack)
if attachment.StackName != nil {
logger := logger.WithValues(logging.StackName, *attachment.StackName)
// Test if stack exists and is ready
stack, err := r.StackRepository.Get(ctx, types.NamespacedName{
Namespace: context.Namespace,
Name: *attachment.Stack,
Name: *attachment.StackName,
})
if err != nil {
if k8sErrors.IsNotFound(err) {
Expand Down
64 changes: 32 additions & 32 deletions internal/controller/context_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,23 @@ func (s *ContextControllerTestSuite) TestContextCreation_InvalidSpec() {
}{
{
Spec: v1beta1.ContextSpec{},
Name: "empty spec, missing space or spaceId",
ExpectedErr: `Context.app.spacelift.io "invalid-context" is invalid: spec: Invalid value: "object": only one of space or spaceId should be set`,
Name: "empty spec, missing spaceName or spaceId",
ExpectedErr: `Context.app.spacelift.io "invalid-context" is invalid: spec: Invalid value: "object": only one of spaceName or spaceId should be set`,
},
{
Spec: v1beta1.ContextSpec{
Space: utils.AddressOf("foobar"),
SpaceId: utils.AddressOf("foobar"),
SpaceName: utils.AddressOf("foobar"),
SpaceId: utils.AddressOf("foobar"),
},
Name: "both space and spaceId are set",
ExpectedErr: `Context.app.spacelift.io "invalid-context" is invalid: spec: Invalid value: "object": only one of space or spaceId should be set`,
Name: "both spaceName and spaceId are set",
ExpectedErr: `Context.app.spacelift.io "invalid-context" is invalid: spec: Invalid value: "object": only one of spaceName or spaceId should be set`,
},
{
Spec: v1beta1.ContextSpec{
Space: utils.AddressOf(""),
SpaceName: utils.AddressOf(""),
},
Name: "space empty string",
ExpectedErr: `Context.app.spacelift.io "invalid-context" is invalid: spec.space: Invalid value: "": spec.space in body should be at least 1 chars long`,
ExpectedErr: `Context.app.spacelift.io "invalid-context" is invalid: spec.spaceName: Invalid value: "": spec.spaceName in body should be at least 1 chars long`,
},
{
Spec: v1beta1.ContextSpec{
Expand All @@ -105,59 +105,59 @@ func (s *ContextControllerTestSuite) TestContextCreation_InvalidSpec() {
},
{
Spec: v1beta1.ContextSpec{
Space: utils.AddressOf("foobar"),
SpaceName: utils.AddressOf("foobar"),
Attachments: []v1beta1.Attachment{{}},
},
Name: "empty attachment",
ExpectedErr: `Context.app.spacelift.io "invalid-context" is invalid: spec.attachments[0]: Invalid value: "object": only one of stack or stackId or moduleId should be set`,
ExpectedErr: `Context.app.spacelift.io "invalid-context" is invalid: spec.attachments[0]: Invalid value: "object": only one of stackName or stackId or moduleId should be set`,
},
{
Spec: v1beta1.ContextSpec{
Space: utils.AddressOf("foobar"),
SpaceName: utils.AddressOf("foobar"),
Attachments: []v1beta1.Attachment{{
Stack: utils.AddressOf("foobar"),
StackId: utils.AddressOf("foobar"),
StackName: utils.AddressOf("foobar"),
StackId: utils.AddressOf("foobar"),
}},
},
Name: "attachment with both stack and stackId",
ExpectedErr: `Context.app.spacelift.io "invalid-context" is invalid: spec.attachments[0]: Invalid value: "object": only one of stack or stackId or moduleId should be set`,
ExpectedErr: `Context.app.spacelift.io "invalid-context" is invalid: spec.attachments[0]: Invalid value: "object": only one of stackName or stackId or moduleId should be set`,
},
{
Spec: v1beta1.ContextSpec{
Space: utils.AddressOf("foobar"),
SpaceName: utils.AddressOf("foobar"),
Attachments: []v1beta1.Attachment{{
ModuleId: utils.AddressOf("foobar"),
StackId: utils.AddressOf("foobar"),
}},
},
Name: "attachment with both stackId and moduleId",
ExpectedErr: `Context.app.spacelift.io "invalid-context" is invalid: spec.attachments[0]: Invalid value: "object": only one of stack or stackId or moduleId should be set`,
ExpectedErr: `Context.app.spacelift.io "invalid-context" is invalid: spec.attachments[0]: Invalid value: "object": only one of stackName or stackId or moduleId should be set`,
},

{
Spec: v1beta1.ContextSpec{
Space: utils.AddressOf("foobar"),
SpaceName: utils.AddressOf("foobar"),
Attachments: []v1beta1.Attachment{{
Stack: utils.AddressOf("foobar"),
ModuleId: utils.AddressOf("foobar"),
StackName: utils.AddressOf("foobar"),
ModuleId: utils.AddressOf("foobar"),
}},
},
Name: "attachment with both stack and moduleId",
ExpectedErr: `Context.app.spacelift.io "invalid-context" is invalid: spec.attachments[0]: Invalid value: "object": only one of stack or stackId or moduleId should be set`,
ExpectedErr: `Context.app.spacelift.io "invalid-context" is invalid: spec.attachments[0]: Invalid value: "object": only one of stackName or stackId or moduleId should be set`,
},
{
Spec: v1beta1.ContextSpec{
Space: utils.AddressOf("foobar"),
SpaceName: utils.AddressOf("foobar"),
Attachments: []v1beta1.Attachment{{
Stack: utils.AddressOf(""),
StackName: utils.AddressOf(""),
}},
},
Name: "attachment stack empty",
ExpectedErr: `Context.app.spacelift.io "invalid-context" is invalid: spec.attachments[0].stack: Invalid value: "": spec.attachments[0].stack in body should be at least 1 chars long`,
Name: "attachment stackName empty",
ExpectedErr: `Context.app.spacelift.io "invalid-context" is invalid: spec.attachments[0].stackName: Invalid value: "": spec.attachments[0].stackName in body should be at least 1 chars long`,
},
{
Spec: v1beta1.ContextSpec{
Space: utils.AddressOf("foobar"),
SpaceName: utils.AddressOf("foobar"),
Attachments: []v1beta1.Attachment{{
StackId: utils.AddressOf(""),
}},
Expand All @@ -167,7 +167,7 @@ func (s *ContextControllerTestSuite) TestContextCreation_InvalidSpec() {
},
{
Spec: v1beta1.ContextSpec{
Space: utils.AddressOf("foobar"),
SpaceName: utils.AddressOf("foobar"),
Attachments: []v1beta1.Attachment{{
ModuleId: utils.AddressOf(""),
}},
Expand All @@ -177,7 +177,7 @@ func (s *ContextControllerTestSuite) TestContextCreation_InvalidSpec() {
},
{
Spec: v1beta1.ContextSpec{
Space: utils.AddressOf("foobar"),
SpaceName: utils.AddressOf("foobar"),
Environment: []v1beta1.Environment{
{
Id: "",
Expand All @@ -190,7 +190,7 @@ func (s *ContextControllerTestSuite) TestContextCreation_InvalidSpec() {
},
{
Spec: v1beta1.ContextSpec{
Space: utils.AddressOf("foobar"),
SpaceName: utils.AddressOf("foobar"),
Environment: []v1beta1.Environment{
{
Id: "test",
Expand All @@ -204,7 +204,7 @@ func (s *ContextControllerTestSuite) TestContextCreation_InvalidSpec() {
},
{
Spec: v1beta1.ContextSpec{
Space: utils.AddressOf("foobar"),
SpaceName: utils.AddressOf("foobar"),
MountedFiles: []v1beta1.MountedFile{
{
Id: "",
Expand All @@ -217,7 +217,7 @@ func (s *ContextControllerTestSuite) TestContextCreation_InvalidSpec() {
},
{
Spec: v1beta1.ContextSpec{
Space: utils.AddressOf("foobar"),
SpaceName: utils.AddressOf("foobar"),
MountedFiles: []v1beta1.MountedFile{
{
Id: "test",
Expand Down Expand Up @@ -288,7 +288,7 @@ func (s *ContextControllerTestSuite) TestContextCreation_OK_SpaceNotReady() {
Namespace: "default",
},
Spec: v1beta1.ContextSpec{
Space: utils.AddressOf("test-space"),
SpaceName: utils.AddressOf("test-space"),
},
}
s.Logs.TakeAll()
Expand Down Expand Up @@ -361,7 +361,7 @@ func (s *ContextControllerTestSuite) TestContextCreation_OK_AttachedStackNotRead
SpaceId: utils.AddressOf("test-space"),
Attachments: []v1beta1.Attachment{
{
Stack: utils.AddressOf("test-stack"),
StackName: utils.AddressOf("test-stack"),
},
},
},
Expand Down
28 changes: 16 additions & 12 deletions internal/spacelift/repository/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ func NewContextRepository(client client.Client) *contextRepository {
return &contextRepository{client: client}
}

type contextCreateMutation struct {
ContextCreate struct {
Id string `graphql:"id"`
} `graphql:"contextCreateV2(input: $input)"`
}

func (r *contextRepository) Create(ctx context.Context, context *v1beta1.Context) (*models.Context, error) {
c, err := spaceliftclient.GetSpaceliftClient(ctx, r.client, context.Namespace)
c, err := spaceliftclient.DefaultClient(ctx, r.client, context.Namespace)
if err != nil {
return nil, errors.Wrap(err, "unable to fetch spacelift client while creating context")
}

var createMutation struct {
ContextCreate struct {
Id string `graphql:"id"`
} `graphql:"contextCreateV2(input: $input)"`
}
var createMutation contextCreateMutation

contextInput, err := structs.FromContextSpec(context)
if err != nil {
Expand All @@ -61,17 +63,19 @@ func (r *contextRepository) Create(ctx context.Context, context *v1beta1.Context
}, nil
}

type contextUpdateMutation struct {
ContextUpdate struct {
Id string `graphql:"id"`
} `graphql:"contextUpdateV2(id: $id, input: $input, replaceConfigElements: $replaceConfigElements)"`
}

func (r *contextRepository) Update(ctx context.Context, context *v1beta1.Context) (*models.Context, error) {
c, err := spaceliftclient.GetSpaceliftClient(ctx, r.client, context.Namespace)
c, err := spaceliftclient.DefaultClient(ctx, r.client, context.Namespace)
if err != nil {
return nil, errors.Wrap(err, "unable to fetch spacelift client while creating context")
}

var updateMutation struct {
ContextUpdate struct {
Id string `graphql:"id"`
} `graphql:"contextUpdateV2(id: $id, input: $input, replaceConfigElements: $replaceConfigElements)"`
}
var updateMutation contextUpdateMutation

contextInput, err := structs.FromContextSpec(context)
if err != nil {
Expand Down
Loading