-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor controller architecture and improve package seperation (#27)
This patch refactors the controller architecture, improves schema handling, and enhances the overall structure of the codebase. It separates concerns more clearly, and introduces a more flexible **dynamiccontroller,** implemenatation and improves various aspects of **CRD** management and schema processing. In brief: - Restructure packages for clearer separation of concerns: - Move `ResourceGroup` controller to its own controller/resourcegroup - Separate `Instance` controller logic from `ResourceGroup` and move to controller/instance - Move some schema related functionality in resourcegroup package - Refactor `DynamicController` to use abstract Handler interface: - Remove direct dependency on graph execution logic - Enhance generic reconciliation process - Addedmetrics for `DynamicController` operations - Enhance schema processing and CRD generation: - Move CRD builds/creation to `ResourceGroup` builder - Improve schema validation and transformation logic - Add `observedGeneration` to default status fields in instance schemas - Add wait for ready functionality to the CRD manager (dealing with some eventual consistency issues) - Update client initialization to return multiple clients - Improve error handling, logging, and reporting throughout the codebase By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
- Loading branch information
Showing
27 changed files
with
1,050 additions
and
1,360 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
examples/deployment-service-ingress/deployment-service-ingress-instance.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
apiVersion: x.symphony.k8s.aws/v1alpha1 | ||
kind: Application | ||
metadata: | ||
name: my-deployment-and-service3 | ||
name: my-deployment-and-service6 | ||
namespace: test | ||
spec: | ||
name: app3 | ||
name: app7 | ||
image: terraform6 |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file 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 instance | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/go-logr/logr" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/dynamic" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
|
||
"github.com/aws-controllers-k8s/symphony/internal/k8smetadata" | ||
"github.com/aws-controllers-k8s/symphony/internal/resourcegroup" | ||
) | ||
|
||
// Controller structure | ||
type Controller struct { | ||
log logr.Logger | ||
gvr schema.GroupVersionResource | ||
client dynamic.Interface | ||
rg *resourcegroup.ResourceGroup | ||
instanceLabeler k8smetadata.Labeler | ||
} | ||
|
||
// NewController creates a new Controller instance | ||
func NewController( | ||
log logr.Logger, | ||
gvr schema.GroupVersionResource, | ||
rg *resourcegroup.ResourceGroup, | ||
client dynamic.Interface, | ||
instanceLabeler k8smetadata.Labeler, | ||
) *Controller { | ||
return &Controller{ | ||
log: log, | ||
gvr: gvr, | ||
client: client, | ||
rg: rg, | ||
instanceLabeler: instanceLabeler, | ||
} | ||
} | ||
|
||
// NewGraphExecReconciler is the main reconciliation loop for the Controller | ||
func (c *Controller) Reconcile(ctx context.Context, req ctrl.Request) error { | ||
namespace, name := getNamespaceName(req) | ||
|
||
log := c.log.WithValues("namespace", namespace, "name", name) | ||
|
||
instance, err := c.client.Resource(c.gvr).Namespace(namespace).Get(ctx, name, metav1.GetOptions{}) | ||
if err != nil { | ||
if apierrors.IsNotFound(err) { | ||
log.Info("Instance not found, it may have been deleted") | ||
return nil | ||
} | ||
log.Error(err, "Failed to get instance") | ||
return nil | ||
} | ||
|
||
rgRuntime, err := c.rg.NewRuntime(instance) | ||
if err != nil { | ||
return fmt.Errorf("failed to create runtime resource group: %w", err) | ||
} | ||
|
||
instanceSubResourcesLabeler, err := k8smetadata.NewInstanceLabeler(instance).Merge(c.instanceLabeler) | ||
if err != nil { | ||
return fmt.Errorf("failed to create instance sub-resources labeler: %w", err) | ||
} | ||
|
||
graphExecReconciler := &InstanceGraphReconciler{ | ||
log: log, | ||
gvr: c.gvr, | ||
client: c.client, | ||
rg: c.rg, | ||
runtime: rgRuntime, | ||
originalRequest: req, | ||
instanceLabeler: c.instanceLabeler, | ||
instanceSubResourcesLabeler: instanceSubResourcesLabeler, | ||
} | ||
return graphExecReconciler.Reconcile(ctx) | ||
} |
Oops, something went wrong.