Skip to content

Commit

Permalink
Add recipe controller
Browse files Browse the repository at this point in the history
This change adds the Kubernetes reconciler for Recipes along with the integration tests.
  • Loading branch information
rynowak committed Oct 9, 2023
1 parent 9985ae2 commit a52e5a2
Show file tree
Hide file tree
Showing 12 changed files with 2,060 additions and 18 deletions.
14 changes: 12 additions & 2 deletions deploy/Chart/crds/radius/radapp.io_recipes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,18 @@ spec:
format: int64
type: integer
operation:
description: Operation is the operation URL of an operation in progress.
type: string
description: Operation tracks the status of an in-progress provisioning
operation.
properties:
operationKind:
description: OperationKind describes the type of operation being
performed.
type: string
resumeToken:
description: ResumeToken is a token that can be used to resume
an in-progress provisioning operation.
type: string
type: object
phrase:
description: Phrase indicates the current status of the Recipe.
type: string
Expand Down
37 changes: 22 additions & 15 deletions pkg/controller/api/radapp.io/v1alpha3/recipe_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package v1alpha3

import (
"net/http"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -75,9 +77,9 @@ type RecipeStatus struct {
// +kubebuilder:validation:Optional
Resource string `json:"resource,omitempty"`

// Operation is the operation URL of an operation in progress.
// Operation tracks the status of an in-progress provisioning operation.
// +kubebuilder:validation:Optional
Operation string `json:"operation,omitempty"`
Operation *ResourceOperation `json:"operation,omitempty"`

// Phrase indicates the current status of the Recipe.
// +kubebuilder:validation:Optional
Expand All @@ -88,21 +90,26 @@ type RecipeStatus struct {
Secret corev1.ObjectReference `json:"secret,omitempty"`
}

func (r *Recipe) SetDefaults() {
if r.Status.Scope == "" {
r.Status.Scope = "/planes/radius/local/resourceGroups/default"
}
if r.Status.Environment == "" {
r.Status.Environment = r.Status.Scope + "/providers/Applications.Core/environments/" + "default"
}
if r.Status.Application == "" {
r.Status.Application = r.Status.Scope + "/providers/Applications.Core/applications/" + r.Namespace
}
if r.Status.Resource == "" {
r.Status.Resource = r.Status.Scope + "/providers/" + r.Spec.Type + "/" + r.Name
}
// ResourceOperation describes the status of an in-progress provisioning operation.
type ResourceOperation struct {
// ResumeToken is a token that can be used to resume an in-progress provisioning operation.
ResumeToken string `json:"resumeToken,omitempty"`

// OperationKind describes the type of operation being performed.
OperationKind OperationKind `json:"operationKind,omitempty"`
}

// OperationKind is the type of operation being performed.
type OperationKind string

const (
// OperationKindPut is a PUT (create or update) operation.
OperationKindPut = http.MethodPut

// OperationKindDelete is a DELETE operation.
OperationKindDelete = http.MethodDelete
)

//+kubebuilder:object:root=true
//+kubebuilder:resource:categories={"all","radius"}
//+kubebuilder:printcolumn:name="Type",type="string",JSONPath=".spec.type",description="Type of resource the recipe should create"
Expand Down
22 changes: 21 additions & 1 deletion pkg/controller/api/radapp.io/v1alpha3/zz_generated.deepcopy.go

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

252 changes: 252 additions & 0 deletions pkg/controller/reconciler/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
/*
Copyright 2023.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package reconciler

import (
"context"
"net/http"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
aztoken "github.com/radius-project/radius/pkg/azure/tokencredentials"
"github.com/radius-project/radius/pkg/cli/clients_new/generated"
corerpv20231001preview "github.com/radius-project/radius/pkg/corerp/api/v20231001preview"
"github.com/radius-project/radius/pkg/sdk"
ucpv20231001preview "github.com/radius-project/radius/pkg/ucp/api/v20231001preview"
"github.com/radius-project/radius/pkg/ucp/resources"
)

type Poller[T any] interface {
Done() bool
Poll(ctx context.Context) (*http.Response, error)
Result(ctx context.Context) (T, error)
ResumeToken() (string, error)
}

var _ Poller[corerpv20231001preview.ContainersClientCreateOrUpdateResponse] = (*runtime.Poller[corerpv20231001preview.ContainersClientCreateOrUpdateResponse])(nil)

type RadiusClient interface {
Applications(scope string) ApplicationClient
Containers(scope string) ContainerClient
Environments(scope string) EnvironmentClient
Groups(scope string) ResourceGroupClient
Resources(scope string, resourceType string) ResourceClient
}

type ApplicationClient interface {
CreateOrUpdate(ctx context.Context, applicationName string, resource corerpv20231001preview.ApplicationResource, options *corerpv20231001preview.ApplicationsClientCreateOrUpdateOptions) (corerpv20231001preview.ApplicationsClientCreateOrUpdateResponse, error)
Delete(ctx context.Context, applicationName string, options *corerpv20231001preview.ApplicationsClientDeleteOptions) (corerpv20231001preview.ApplicationsClientDeleteResponse, error)
Get(ctx context.Context, applicationName string, options *corerpv20231001preview.ApplicationsClientGetOptions) (corerpv20231001preview.ApplicationsClientGetResponse, error)
}

type ContainerClient interface {
BeginCreateOrUpdate(ctx context.Context, containerName string, resource corerpv20231001preview.ContainerResource, options *corerpv20231001preview.ContainersClientBeginCreateOrUpdateOptions) (Poller[corerpv20231001preview.ContainersClientCreateOrUpdateResponse], error)
BeginDelete(ctx context.Context, containerName string, options *corerpv20231001preview.ContainersClientBeginDeleteOptions) (Poller[corerpv20231001preview.ContainersClientDeleteResponse], error)
ContinueCreateOperation(ctx context.Context, resumeToken string) (Poller[corerpv20231001preview.ContainersClientCreateOrUpdateResponse], error)
ContinueDeleteOperation(ctx context.Context, resumeToken string) (Poller[corerpv20231001preview.ContainersClientDeleteResponse], error)
Get(ctx context.Context, containerName string, options *corerpv20231001preview.ContainersClientGetOptions) (corerpv20231001preview.ContainersClientGetResponse, error)
}

type EnvironmentClient interface {
List(ctx context.Context, options *corerpv20231001preview.EnvironmentsClientListByScopeOptions) (corerpv20231001preview.EnvironmentsClientListByScopeResponse, error)
}

type ResourceGroupClient interface {
CreateOrUpdate(ctx context.Context, resourceGroupName string, resource ucpv20231001preview.ResourceGroupResource, options *ucpv20231001preview.ResourceGroupsClientCreateOrUpdateOptions) (ucpv20231001preview.ResourceGroupsClientCreateOrUpdateResponse, error)
Get(ctx context.Context, resourceGroupName string, options *ucpv20231001preview.ResourceGroupsClientGetOptions) (ucpv20231001preview.ResourceGroupsClientGetResponse, error)
}

type ResourceClient interface {
BeginCreateOrUpdate(ctx context.Context, resourceName string, resource generated.GenericResource, options *generated.GenericResourcesClientBeginCreateOrUpdateOptions) (Poller[generated.GenericResourcesClientCreateOrUpdateResponse], error)
BeginDelete(ctx context.Context, resourceName string, options *generated.GenericResourcesClientBeginDeleteOptions) (Poller[generated.GenericResourcesClientDeleteResponse], error)
ContinueCreateOperation(ctx context.Context, resumeToken string) (Poller[generated.GenericResourcesClientCreateOrUpdateResponse], error)
ContinueDeleteOperation(ctx context.Context, resumeToken string) (Poller[generated.GenericResourcesClientDeleteResponse], error)
Get(ctx context.Context, resourceName string) (generated.GenericResourcesClientGetResponse, error)
ListSecrets(ctx context.Context, resourceName string) (generated.GenericResourcesClientListSecretsResponse, error)
}

type Client struct {
connection sdk.Connection
}

func NewClient(connection sdk.Connection) *Client {
return &Client{connection: connection}
}

var _ RadiusClient = (*Client)(nil)

func (c *Client) Applications(scope string) ApplicationClient {
ac, err := corerpv20231001preview.NewApplicationsClient(scope, &aztoken.AnonymousCredential{}, sdk.NewClientOptions(c.connection))
if err != nil {
panic("failed to create client: " + err.Error())
}

return &ApplicationClientImpl{inner: ac}
}

func (c *Client) Containers(scope string) ContainerClient {
cc, err := corerpv20231001preview.NewContainersClient(scope, &aztoken.AnonymousCredential{}, sdk.NewClientOptions(c.connection))
if err != nil {
panic("failed to create client: " + err.Error())
}

return &ContainerClientImpl{inner: cc}
}

func (c *Client) Environments(scope string) EnvironmentClient {
ec, err := corerpv20231001preview.NewEnvironmentsClient(scope, &aztoken.AnonymousCredential{}, sdk.NewClientOptions(c.connection))
if err != nil {
panic("failed to create client: " + err.Error())
}

return &EnvironmentClientImpl{inner: ec}
}

func (c *Client) Groups(scope string) ResourceGroupClient {
rgc, err := ucpv20231001preview.NewResourceGroupsClient(&aztoken.AnonymousCredential{}, sdk.NewClientOptions(c.connection))
if err != nil {
panic("failed to create client: " + err.Error())
}

return &ResourceGroupClientImpl{inner: rgc, scope: scope}
}

func (c *Client) Resources(scope string, resourceType string) ResourceClient {
gc, err := generated.NewGenericResourcesClient(scope, resourceType, &aztoken.AnonymousCredential{}, sdk.NewClientOptions(c.connection))
if err != nil {
panic("failed to create client: " + err.Error())
}

return &ResourceClientImpl{inner: gc}
}

var _ ApplicationClient = (*ApplicationClientImpl)(nil)

type ApplicationClientImpl struct {
inner *corerpv20231001preview.ApplicationsClient
}

func (ac *ApplicationClientImpl) CreateOrUpdate(ctx context.Context, applicationName string, resource corerpv20231001preview.ApplicationResource, options *corerpv20231001preview.ApplicationsClientCreateOrUpdateOptions) (corerpv20231001preview.ApplicationsClientCreateOrUpdateResponse, error) {
return ac.inner.CreateOrUpdate(ctx, applicationName, resource, options)
}

func (ac *ApplicationClientImpl) Delete(ctx context.Context, applicationName string, options *corerpv20231001preview.ApplicationsClientDeleteOptions) (corerpv20231001preview.ApplicationsClientDeleteResponse, error) {
return ac.inner.Delete(ctx, applicationName, options)
}

func (ac *ApplicationClientImpl) Get(ctx context.Context, applicationName string, options *corerpv20231001preview.ApplicationsClientGetOptions) (corerpv20231001preview.ApplicationsClientGetResponse, error) {
return ac.inner.Get(ctx, applicationName, options)
}

var _ ContainerClient = (*ContainerClientImpl)(nil)

type ContainerClientImpl struct {
inner *corerpv20231001preview.ContainersClient
}

func (cc *ContainerClientImpl) BeginCreateOrUpdate(ctx context.Context, containerName string, resource corerpv20231001preview.ContainerResource, options *corerpv20231001preview.ContainersClientBeginCreateOrUpdateOptions) (Poller[corerpv20231001preview.ContainersClientCreateOrUpdateResponse], error) {
return cc.inner.BeginCreateOrUpdate(ctx, containerName, resource, options)
}

func (cc *ContainerClientImpl) BeginDelete(ctx context.Context, containerName string, options *corerpv20231001preview.ContainersClientBeginDeleteOptions) (Poller[corerpv20231001preview.ContainersClientDeleteResponse], error) {
return cc.inner.BeginDelete(ctx, containerName, options)
}

func (cc *ContainerClientImpl) ContinueCreateOperation(ctx context.Context, resumeToken string) (Poller[corerpv20231001preview.ContainersClientCreateOrUpdateResponse], error) {
return cc.inner.BeginCreateOrUpdate(ctx, "", corerpv20231001preview.ContainerResource{}, &corerpv20231001preview.ContainersClientBeginCreateOrUpdateOptions{ResumeToken: resumeToken})
}

func (cc *ContainerClientImpl) ContinueDeleteOperation(ctx context.Context, resumeToken string) (Poller[corerpv20231001preview.ContainersClientDeleteResponse], error) {
return cc.inner.BeginDelete(ctx, "", &corerpv20231001preview.ContainersClientBeginDeleteOptions{ResumeToken: resumeToken})
}

func (cc *ContainerClientImpl) Get(ctx context.Context, containerName string, options *corerpv20231001preview.ContainersClientGetOptions) (corerpv20231001preview.ContainersClientGetResponse, error) {
return cc.inner.Get(ctx, containerName, options)
}

var _ EnvironmentClient = (*EnvironmentClientImpl)(nil)

type EnvironmentClientImpl struct {
inner *corerpv20231001preview.EnvironmentsClient
}

func (ec *EnvironmentClientImpl) List(ctx context.Context, options *corerpv20231001preview.EnvironmentsClientListByScopeOptions) (corerpv20231001preview.EnvironmentsClientListByScopeResponse, error) {
result := corerpv20231001preview.EnvironmentsClientListByScopeResponse{}
pager := ec.inner.NewListByScopePager(options)
for pager.More() {
response, err := pager.NextPage(ctx)
if err != nil {
return corerpv20231001preview.EnvironmentsClientListByScopeResponse{}, err
}

result.Value = append(result.Value, response.Value...)
}

return result, nil
}

type ResourceGroupClientImpl struct {
inner *ucpv20231001preview.ResourceGroupsClient
scope string
}

func (rgc *ResourceGroupClientImpl) CreateOrUpdate(ctx context.Context, resourceGroupName string, resource ucpv20231001preview.ResourceGroupResource, options *ucpv20231001preview.ResourceGroupsClientCreateOrUpdateOptions) (ucpv20231001preview.ResourceGroupsClientCreateOrUpdateResponse, error) {
parsed, err := resources.ParseScope(rgc.scope)
if err != nil {
return ucpv20231001preview.ResourceGroupsClientCreateOrUpdateResponse{}, err
}

return rgc.inner.CreateOrUpdate(ctx, "radius", parsed.FindScope("radius"), resourceGroupName, resource, options)
}

func (rgc *ResourceGroupClientImpl) Get(ctx context.Context, resourceGroupName string, options *ucpv20231001preview.ResourceGroupsClientGetOptions) (ucpv20231001preview.ResourceGroupsClientGetResponse, error) {
parsed, err := resources.ParseScope(rgc.scope)
if err != nil {
return ucpv20231001preview.ResourceGroupsClientGetResponse{}, err
}

return rgc.inner.Get(ctx, "radius", parsed.FindScope("radius"), resourceGroupName, options)
}

var _ ResourceClient = (*ResourceClientImpl)(nil)

type ResourceClientImpl struct {
inner *generated.GenericResourcesClient
}

func (rc *ResourceClientImpl) BeginCreateOrUpdate(ctx context.Context, resourceName string, resource generated.GenericResource, options *generated.GenericResourcesClientBeginCreateOrUpdateOptions) (Poller[generated.GenericResourcesClientCreateOrUpdateResponse], error) {
return rc.inner.BeginCreateOrUpdate(ctx, resourceName, resource, options)
}

func (rc *ResourceClientImpl) BeginDelete(ctx context.Context, resourceName string, options *generated.GenericResourcesClientBeginDeleteOptions) (Poller[generated.GenericResourcesClientDeleteResponse], error) {
return rc.inner.BeginDelete(ctx, resourceName, options)
}

func (rc *ResourceClientImpl) ContinueCreateOperation(ctx context.Context, resumeToken string) (Poller[generated.GenericResourcesClientCreateOrUpdateResponse], error) {
return rc.inner.BeginCreateOrUpdate(ctx, "", generated.GenericResource{}, &generated.GenericResourcesClientBeginCreateOrUpdateOptions{ResumeToken: resumeToken})
}

func (rc *ResourceClientImpl) ContinueDeleteOperation(ctx context.Context, resumeToken string) (Poller[generated.GenericResourcesClientDeleteResponse], error) {
return rc.inner.BeginDelete(ctx, "", &generated.GenericResourcesClientBeginDeleteOptions{ResumeToken: resumeToken})
}

func (rc *ResourceClientImpl) Get(ctx context.Context, resourceName string) (generated.GenericResourcesClientGetResponse, error) {
return rc.inner.Get(ctx, resourceName, nil)
}

func (rc *ResourceClientImpl) ListSecrets(ctx context.Context, resourceName string) (generated.GenericResourcesClientListSecretsResponse, error) {
return rc.inner.ListSecrets(ctx, resourceName, nil)
}
Loading

0 comments on commit a52e5a2

Please sign in to comment.