Skip to content
Open
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
4 changes: 3 additions & 1 deletion go/deployment-operator/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ VELERO_CHART_URL := https://github.com/vmware-tanzu/helm-charts/releases/downloa

ENVTEST_K8S_VERSION := 1.35.0
CONTROLLER_GEN_PATHS ?= ./api/...;./internal/...;./pkg/...
CONTROLLER_GEN_CRD_PATHS ?= ./api/...

PRE = --ensure

Expand All @@ -44,7 +45,8 @@ PRE = --ensure
.PHONY: manifests
manifests: TOOL = controller-gen
manifests: --tool ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="$(CONTROLLER_GEN_PATHS)" output:crd:artifacts:config=config/crd/bases
$(CONTROLLER_GEN) rbac:roleName=manager-role webhook paths="$(CONTROLLER_GEN_PATHS)"
$(CONTROLLER_GEN) crd paths="$(CONTROLLER_GEN_CRD_PATHS)" output:crd:artifacts:config=config/crd/bases
@$(MAKE) -s codegen-chart-crds

.PHONY: generate
Expand Down
152 changes: 152 additions & 0 deletions go/deployment-operator/api/v1alpha1/pluralcrossplanecluster_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package v1alpha1

import (
"context"
"fmt"

console "github.com/pluralsh/console/go/client"
"github.com/samber/lo"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sClient "sigs.k8s.io/controller-runtime/pkg/client"
)

func init() {
SchemeBuilder.Register(&PluralCrossplaneClusterList{}, &PluralCrossplaneCluster{})
}

// PluralCrossplanelusterList contains a list of [PluralCrossplaneCluster]
// +kubebuilder:object:root=true
type PluralCrossplaneClusterList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []PluralCrossplaneCluster `json:"items"`
}

// PluralCrossplaneCluster is the Schema for the Crossplane cluster configuration
// +kubebuilder:object:root=true
// +kubebuilder:resource:scope=Namespaced
// +kubebuilder:subresource:status
type PluralCrossplaneCluster struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

// Spec of the Crossplane cluster configuration
// +kubebuilder:validation:Required
Spec CrossplaneConfigurationClusterSpec `json:"spec"`

// Status of the Crossplane cluster configuration
// +kubebuilder:validation:Optional
Status Status `json:"status,omitempty"`
}

type CrossplaneConfigurationClusterSpec struct {
// Cluster is a simplified representation of the Console API cluster
// object. See [ClusterSpec] for more information.
// +kubebuilder:validation:Optional
Cluster *ClusterSpec `json:"cluster,omitempty"`

// TokenSecretRef contains the reference to the secret holding the token to access the Console API
// +kubebuilder:validation:Required
ConsoleTokenSecretRef corev1.SecretKeySelector `json:"consoleTokenSecretRef"`

// CrossplaneClusterRef contains the reference to the Crossplane cluster
// +kubebuilder:validation:Required
CrossplaneClusterRef corev1.ObjectReference `json:"crossplaneClusterRef"`

// Agent allows configuring agent specific helm chart options.
// +kubebuilder:validation:Optional
Agent *AgentHelmConfiguration `json:"agent,omitempty"`
}

func (in *CrossplaneConfigurationClusterSpec) GetAgent() *AgentHelmConfiguration {
if in == nil || in.Agent == nil {
return &AgentHelmConfiguration{
HelmConfiguration: HelmConfiguration{
RepoUrl: lo.ToPtr(AgentDefaultRepository),
ChartName: lo.ToPtr(AgentDefaultChartName),
},
}
}

return in.Agent
}

func (in *PluralCrossplaneCluster) Diff(hasher Hasher) (changed bool, sha string, err error) {
currentSha, err := hasher(in.Spec)
if err != nil {
return false, "", err
}

return !in.Status.IsSHAEqual(currentSha), currentSha, nil
}

func (in *PluralCrossplaneCluster) SetCondition(condition metav1.Condition) {
meta.SetStatusCondition(&in.Status.Conditions, condition)
}

func (in *PluralCrossplaneCluster) ClusterName() string {
if in.Spec.Cluster != nil && in.Spec.Cluster.Handle != nil {
return lo.FromPtr(in.Spec.Cluster.Handle)
}
return in.Name
}

func (in *PluralCrossplaneCluster) GetConsoleToken(ctx context.Context, c k8sClient.Client) (string, error) {
secret := &corev1.Secret{}

if err := c.Get(
ctx,
k8sClient.ObjectKey{Name: in.Spec.ConsoleTokenSecretRef.Name, Namespace: in.Namespace},
secret,
); err != nil {
return "", err
}

token, exists := secret.Data[in.Spec.ConsoleTokenSecretRef.Key]
if !exists {
return "", fmt.Errorf("secret %s/%s does not contain console token", in.Namespace, in.Spec.ConsoleTokenSecretRef.Name)
}

return string(token), nil
}

func (in *PluralCrossplaneCluster) Attributes() console.ClusterAttributes {
attrs := console.ClusterAttributes{
Name: in.Name,
}

if in.Spec.Cluster == nil {
return attrs
}

if in.Spec.Cluster.Handle != nil {
attrs.Handle = in.Spec.Cluster.Handle
}

if in.Spec.Cluster.Tags != nil {
tags := make([]*console.TagAttributes, 0)
for name, value := range in.Spec.Cluster.Tags {
tags = append(tags, &console.TagAttributes{Name: name, Value: value})
}
attrs.Tags = tags
}

if in.Spec.Cluster.Metadata != nil {
attrs.Metadata = lo.ToPtr(string(in.Spec.Cluster.Metadata.Raw))
}

if in.Spec.Cluster.Bindings != nil {
attrs.ReadBindings = PolicyBindings(in.Spec.Cluster.Bindings.Read)
attrs.WriteBindings = PolicyBindings(in.Spec.Cluster.Bindings.Write)
}

return attrs
}

// UpdateAttributes returns the update attributes for this CR by converting
// the value returned from Attributes().
func (in *PluralCrossplaneCluster) UpdateAttributes() console.ClusterUpdateAttributes {
return ConvertClusterAttributesToUpdate(in.Attributes())
}
98 changes: 97 additions & 1 deletion go/deployment-operator/api/v1alpha1/zz_generated.deepcopy.go

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

7 changes: 7 additions & 0 deletions go/deployment-operator/cmd/agent/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,13 @@ func registerKubeReconcilersOrDie(
}).SetupWithManager(manager); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "PluralCAPIClusterController")
}
if err := (&controller.PluralCrossplaneClusterController{
Client: manager.GetClient(),
Scheme: manager.GetScheme(),
ConsoleUrl: consoleURL,
}).SetupWithManager(manager); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "PluralCrossplaneClusterController")
}
if err := (&controller.SentinelRunJobReconciler{
Client: manager.GetClient(),
Scheme: manager.GetScheme(),
Expand Down
2 changes: 2 additions & 0 deletions go/deployment-operator/cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
ctrl "sigs.k8s.io/controller-runtime"

"github.com/pluralsh/console/go/deployment-operator/internal/crossplane"
"github.com/pluralsh/console/go/deployment-operator/internal/utils"
"github.com/pluralsh/console/go/deployment-operator/pkg/cache"
discoverycache "github.com/pluralsh/console/go/deployment-operator/pkg/cache/discovery"
Expand Down Expand Up @@ -66,6 +67,7 @@ func init() {
utilruntime.Must(openshift.AddToScheme(scheme))
utilruntime.Must(fluxcd.AddToScheme(scheme))
utilruntime.Must(clusterv1.AddToScheme(scheme))
utilruntime.Must(crossplane.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,24 @@ spec:
spec:
description: AgentRunSpec defines the desired state of AgentRun
properties:
branch:
description: Branch is the repository branch the agent should operate
on. If omitted, the repository default branch is used.
type: string
flowId:
description: FlowID is the flow this agent run is associated with
(optional)
type: string
language:
description: |-
Language is the programming language used in the agent run.

Deprecated: No longer used for image selection. Enable dind on the AgentRuntime instead.
type: string
languageVersion:
description: |-
LanguageVersion is the version of the language to use, if you wish to specify.

Deprecated: No longer used for image selection. Enable dind on the AgentRuntime instead.
type: string
mode:
Expand All @@ -68,10 +74,6 @@ spec:
description: Repository is the git repository the agent will work
with
type: string
branch:
description: Branch is the repository branch the agent should operate
on. If omitted, the repository default branch is used.
type: string
runtimeRef:
properties:
name:
Expand Down
Loading
Loading