Skip to content

Commit

Permalink
added civoclient and fixed cluster update via meta.externalname
Browse files Browse the repository at this point in the history
  • Loading branch information
vladfr committed Feb 17, 2023
1 parent 87f1c47 commit 4300017
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 17 deletions.
4 changes: 4 additions & 0 deletions apis/civo/cluster/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ type CivoKubernetesSpec struct {
// A list of applications to install from civo marketplace.
Applications []string `json:"applications,omitempty"`
ConnectionDetails CivoKubernetesConnectionDetails `json:"connectionDetails"`
// +required
// +immutable
// NOTE: This can only be set at creation time. Changing this value after creation will not move the cluster into another network..
NetworkID *string `json:"networkId"`
// +optional
// +kubebuilder:validation:Enum=flannel;cilium
// +kubebuilder:default=flannel
Expand Down
5 changes: 5 additions & 0 deletions apis/civo/cluster/v1alpha1/zz_generated.deepcopy.go

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

2 changes: 1 addition & 1 deletion build
14 changes: 14 additions & 0 deletions internal/civoclient/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package civoclient

import (
"github.com/civo/civogo"
)

const (
APIURL = "https://api.civo.com"
)

// CivoAPIClient returns a civogo client using the current default API key
func NewAPIClient(APIKey, region string) (*civogo.Client, error) {
return civogo.NewClientWithURL(APIKey, APIURL, region)
}
55 changes: 39 additions & 16 deletions internal/controller/civokubernetes/civokubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
"github.com/civo/civogo"
"github.com/crossplane-contrib/provider-civo/apis/civo/cluster/v1alpha1"
v1alpha1provider "github.com/crossplane-contrib/provider-civo/apis/civo/provider/v1alpha1"
"github.com/crossplane-contrib/provider-civo/pkg/civocli"
"github.com/crossplane-contrib/provider-civo/internal/civoclient"
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/meta"
"github.com/crossplane/crossplane-runtime/pkg/ratelimiter"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"
Expand All @@ -38,7 +39,7 @@ type connecter struct {

type external struct {
kube client.Client
civoClient *civocli.CivoClient
civoClient *civogo.Client
}

// Setup sets up a Civo Kubernetes controller.
Expand Down Expand Up @@ -84,7 +85,7 @@ func (c *connecter) Connect(ctx context.Context, mg resource.Managed) (managed.E
return nil, errors.New("could not find secret")
}

civoClient, err := civocli.NewCivoClient(string(s.Data["credentials"]), providerConfig.Spec.Region)
civoClient, err := civoclient.NewAPIClient(string(s.Data["credentials"]), providerConfig.Spec.Region)

if err != nil {
return nil, err
Expand All @@ -95,15 +96,15 @@ func (c *connecter) Connect(ctx context.Context, mg resource.Managed) (managed.E
}, nil
}

//nolint
// nolint
func (e *external) Observe(ctx context.Context, mg resource.Managed) (managed.ExternalObservation, error) {
cr, ok := mg.(*v1alpha1.CivoKubernetes)
if !ok {
return managed.ExternalObservation{}, errors.New("invalid object")
}
civoCluster, err := e.civoClient.GetK3sCluster(cr.Spec.Name)
civoCluster, err := e.civoClient.GetKubernetesCluster(meta.GetExternalName(cr.GetObjectMeta()))
if err != nil {
return managed.ExternalObservation{ResourceExists: false}, err
return managed.ExternalObservation{}, nil
}
if civoCluster == nil {
return managed.ExternalObservation{ResourceExists: false}, nil
Expand Down Expand Up @@ -170,19 +171,32 @@ func (e *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext
if !ok {
return managed.ExternalCreation{}, errors.New("invalid object")
}
civoCluster, err := e.civoClient.GetK3sCluster(cr.Spec.Name)
if err != nil {
return managed.ExternalCreation{}, err
}
civoCluster, _ := e.civoClient.GetKubernetesCluster(meta.GetExternalName(cr.GetObjectMeta()))
// TODO treat error case VS cluster not exists
// if err != nil {
// return managed.ExternalCreation{}, err
// }
if civoCluster != nil {
return managed.ExternalCreation{}, nil
}
// Create or Update
err = e.civoClient.CreateNewK3sCluster(cr.Spec.Name, cr.Spec.Pools, cr.Spec.Applications, cr.Spec.CNIPlugin, cr.Spec.Version)
kc := &civogo.KubernetesClusterConfig{
Name: cr.Spec.Name,
Pools: cr.Spec.Pools,
Applications: "",
CNIPlugin: *cr.Spec.CNIPlugin,
KubernetesVersion: *cr.Spec.Version,
Region: e.civoClient.Region,
NetworkID: *cr.Spec.NetworkID,
}
newCluster, err := e.civoClient.NewKubernetesClusters(kc)
if err != nil {
log.Warn("Cluster creation failed", err)
return managed.ExternalCreation{}, err
}

meta.SetExternalName(cr, newCluster.ID)

cr.SetConditions(xpv1.Creating())

return managed.ExternalCreation{
Expand All @@ -196,7 +210,7 @@ func (e *external) Update(ctx context.Context, mg resource.Managed) (managed.Ext
if !ok {
return managed.ExternalUpdate{}, errors.New("invalid object")
}
remoteCivoCluster, err := e.civoClient.GetK3sCluster(desiredCivoCluster.Spec.Name)
remoteCivoCluster, err := e.civoClient.GetKubernetesCluster(meta.GetExternalName(desiredCivoCluster.GetObjectMeta()))
if err != nil {
return managed.ExternalUpdate{}, err
}
Expand All @@ -216,15 +230,23 @@ func (e *external) Update(ctx context.Context, mg resource.Managed) (managed.Ext

log.Debug("Pools are not equal")
//TODO: Set region in the civo client once to avoid passing the providerConfig
if err := e.civoClient.UpdateK3sCluster(desiredCivoCluster, remoteCivoCluster, providerConfig); err != nil {
desiredClusterConfig := &civogo.KubernetesClusterConfig{
Pools: desiredCivoCluster.Spec.Pools,
Region: e.civoClient.Region, // TODO: Region needs to be on the cluster spec
}
if _, err := e.civoClient.UpdateKubernetesCluster(meta.GetExternalName(desiredCivoCluster), desiredClusterConfig); err != nil {
return managed.ExternalUpdate{}, err
}
}

if desiredCivoCluster.Spec.Version != nil {
if *desiredCivoCluster.Spec.Version > remoteCivoCluster.Version {
log.Info("Updating cluster version")
if err := e.civoClient.UpdateK3sClusterVersion(desiredCivoCluster, remoteCivoCluster, providerConfig); err != nil {
desiredClusterConfig := &civogo.KubernetesClusterConfig{
KubernetesVersion: *desiredCivoCluster.Spec.Version,
Region: e.civoClient.Region, // TODO: Region needs to be on the cluster spec
}
if _, err := e.civoClient.UpdateKubernetesCluster(meta.GetExternalName(desiredCivoCluster), desiredClusterConfig); err != nil {
return managed.ExternalUpdate{}, err
}
}
Expand All @@ -238,7 +260,7 @@ func (e *external) Delete(ctx context.Context, mg resource.Managed) error {
if !ok {
return nil
}
civoCluster, err := e.civoClient.GetK3sCluster(cr.Spec.Name)
civoCluster, err := e.civoClient.GetKubernetesCluster(meta.GetExternalName(cr.GetObjectMeta()))
if err != nil {
return err
}
Expand All @@ -262,7 +284,8 @@ func (e *external) Delete(ctx context.Context, mg resource.Managed) error {
// ------------------------------------------------
cr.Status.Message = deletionMessage
cr.SetConditions(xpv1.Deleting())
return e.civoClient.DeleteK3sCluster(civoCluster.Name)
_, err = e.civoClient.DeleteKubernetesCluster(civoCluster.ID)
return err
}

func arePoolsEqual(desiredCivoCluster *v1alpha1.CivoKubernetes, remoteCivoCluster *civogo.KubernetesCluster) bool {
Expand Down
6 changes: 6 additions & 0 deletions package/crds/cluster.civo.crossplane.io_civokubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ spec:
type: string
name:
type: string
networkId:
description: 'NOTE: This can only be set at creation time. Changing
this value after creation will not move the cluster into another
network..'
type: string
pools:
items:
description: KubernetesClusterPoolConfig is used to create a new
Expand Down Expand Up @@ -151,6 +156,7 @@ spec:
required:
- connectionDetails
- name
- networkId
- pools
type: object
status:
Expand Down

0 comments on commit 4300017

Please sign in to comment.