Skip to content

Commit

Permalink
Add recipe controller (#6438)
Browse files Browse the repository at this point in the history
# Description

This change adds the Kubernetes reconciler for Recipes along with the
integration tests.

## Type of change


- This pull request adds or changes features of Radius and has an
approved issue (issue link required).



## Auto-generated summary

<!--
GitHub Copilot for docs will auto-generate a summary of the PR
-->

<!--
copilot:all
-->
### <samp>🤖 Generated by Copilot at be5f8cd</samp>

### Summary
🗑️🌐🧪

<!--
1. 🗑️ - This emoji represents the removal of the SetDefaults method, as
it is no longer needed and simplifies the Recipe type.
2. 🌐 - This emoji represents the addition of the RadiusClient interface
and its implementations, as they provide the communication layer between
the reconciler and the Radius API.
3. 🧪 - This emoji represents the addition of the unit tests for the
RecipeReconciler, as they ensure the correctness and reliability of the
reconciler logic.
-->
This pull request introduces the reconciler package that implements the
logic for managing Recipe objects in the controller service. The
reconciler uses the RadiusClient interface to interact with the Radius
API and create, update, or delete resources and secrets based on the
recipe specifications. The pull request also adds some unit tests and
constants for the reconciler, and removes the unused SetDefaults method
from the Recipe type.

> _`Recipe` objects_
> _Reconciled with Radius_
> _Autumn of secrets_

### Walkthrough
* Remove SetDefaults method from Recipe type
([link](https://github.com/radius-project/radius/pull/6438/files?diff=unified&w=0#diff-5e52978508d6b18ab3b5a2ebd856c3c26372fc6bdefed4542bb1904fa209ae3aL91-L105))
* Define RadiusClient interface and implementations to interact with
Radius API
([link](https://github.com/radius-project/radius/pull/6438/files?diff=unified&w=0#diff-07bbdbc30ae4ea6747d198d1a0ab5a244c6e7bbd559ea5d4f2e1794936c643afR1-R252))
* Define Poller interface and helper types and functions for working
with Radius resources
([link](https://github.com/radius-project/radius/pull/6438/files?diff=unified&w=0#diff-07bbdbc30ae4ea6747d198d1a0ab5a244c6e7bbd559ea5d4f2e1794936c643afR1-R252))
* Define function to convert resource to connection values for recipe
secret data
([link](https://github.com/radius-project/radius/pull/6438/files?diff=unified&w=0#diff-b7077d6be67d0defe31f8815da3903198ab33557fc278d9294ea636c09e4eb3dR1-R65))
* Define constants for annotation keys, polling delay, and finalizer
names
([link](https://github.com/radius-project/radius/pull/6438/files?diff=unified&w=0#diff-1c5d9f9f2bd3272c714eba486f899cd6e053bc2fd950dac921bc328d789568b1R1-R33))
* Set up test environment for reconciler package using envtest and mock
Radius client (`main_test.go`,
[link](https://github.com/radius-project/radius/pull/6438/files?diff=unified&w=0#diff-77d671434b376fb7c1f0f24a606661bb824d6259e8b3c27fa770977f0a63bbd9R1-R80))
* Define utility functions for finding or creating environments,
applications, and resource groups, and creating, updating, deleting, or
fetching resources
([link](https://github.com/radius-project/radius/pull/6438/files?diff=unified&w=0#diff-b2622e855cc3c8af44551c69c158d0400b6b2df3e3ebaefef18f7db96c6b66bbR1-R175))
* Define unit tests for RecipeReconciler for different scenarios
(`recipe_reconciler_test.go`,
[link](https://github.com/radius-project/radius/pull/6438/files?diff=unified&w=0#diff-2b3aa3c5be32be791c9069d44213d653090aed157026f3d37f187ccded049cacR1-R400))
* Set up RecipeReconciler with manager and Radius client in controller
service (`service.go`,
[link](https://github.com/radius-project/radius/pull/6438/files?diff=unified&w=0#diff-b31c03e05358c6faab6e77c96909e14e14eeb0dc09412ede3329625adc197fa9R33),
[link](https://github.com/radius-project/radius/pull/6438/files?diff=unified&w=0#diff-b31c03e05358c6faab6e77c96909e14e14eeb0dc09412ede3329625adc197fa9R86-R94))
  • Loading branch information
rynowak authored Oct 9, 2023
1 parent 9985ae2 commit 16dc239
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 16dc239

Please sign in to comment.