Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrong cluster and instance external name #53

Merged
merged 3 commits into from
Sep 5, 2024
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ running cluster:

```
make generate
kubectl apply -f packages/crds/
kubectl apply -f package/crds/
```

If you need to make changes to the provider Go code, you can Ctrl-C to quit the binary
Expand Down
7 changes: 4 additions & 3 deletions internal/clients/akuity/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/structpb"

"github.com/avast/retry-go/v4"

"github.com/akuity/api-client-go/pkg/api/gateway/accesscontrol"
argocdv1 "github.com/akuity/api-client-go/pkg/api/gen/argocd/v1"
idv1 "github.com/akuity/api-client-go/pkg/api/gen/types/id/v1"
Expand All @@ -21,7 +23,6 @@ import (
"github.com/akuityio/provider-crossplane-akuity/internal/types"
akuitytypes "github.com/akuityio/provider-crossplane-akuity/internal/types/generated/akuity/v1alpha1"
"github.com/akuityio/provider-crossplane-akuity/internal/utils/protobuf"
"github.com/avast/retry-go/v4"
)

const (
Expand Down Expand Up @@ -228,7 +229,7 @@ func (c client) DeleteInstance(ctx context.Context, name string) error {
}

func (c client) BuildApplyInstanceRequest(instance crossplanetypes.Instance) (*argocdv1.ApplyInstanceRequest, error) {
argocdPB, err := types.CrossplaneToAkuityAPIArgoCD(instance.Name, instance.Spec.ForProvider.ArgoCD)
argocdPB, err := types.CrossplaneToAkuityAPIArgoCD(instance.Spec.ForProvider.Name, instance.Spec.ForProvider.ArgoCD)
if err != nil {
return nil, fmt.Errorf("could not marshal instance argocd spec to protobuf: %w", err)
}
Expand Down Expand Up @@ -276,7 +277,7 @@ func (c client) BuildApplyInstanceRequest(instance crossplanetypes.Instance) (*a
request := &argocdv1.ApplyInstanceRequest{
OrganizationId: c.organizationID,
IdType: idv1.Type_NAME,
Id: instance.Name,
Id: instance.Spec.ForProvider.Name,
Argocd: argocdPB,
ArgocdConfigmap: argocdConfigMapPB,
ArgocdRbacConfigmap: argocdRbacConfigMapPB,
Expand Down
17 changes: 10 additions & 7 deletions internal/controller/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ func Setup(mgr ctrl.Manager, o controller.Options) error {
managed.WithLogger(logger),
managed.WithPollInterval(o.PollInterval),
managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))),
managed.WithConnectionPublishers(cps...))
managed.WithConnectionPublishers(cps...),
managed.WithInitializers(),
)

return ctrl.NewControllerManagedBy(mgr).
Named(name).
Expand Down Expand Up @@ -140,19 +142,19 @@ func (c *External) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
return managed.ExternalObservation{}, errors.New(errNotCluster)
}

if meta.GetExternalName(managedCluster) == "" {
return managed.ExternalObservation{
ResourceExists: false,
}, nil
}

instanceID, err := c.getInstanceID(ctx, managedCluster.Spec.ForProvider.InstanceID, managedCluster.Spec.ForProvider.InstanceRef)
if err != nil {
return managed.ExternalObservation{}, err
}

managedCluster.Spec.ForProvider.InstanceID = instanceID

if meta.GetExternalName(managedCluster) == "" {
return managed.ExternalObservation{
ResourceExists: false,
}, nil
}

akuityCluster, err := c.client.GetCluster(ctx, instanceID, meta.GetExternalName(managedCluster))
if err != nil {
c.logger.Debug("As we are not able to differentiate PermissionDenied/NotFound when calling GetCluster, we simply treat as not found here", "error", err)
Expand Down Expand Up @@ -234,6 +236,7 @@ func (c *External) Create(ctx context.Context, mg resource.Managed) (managed.Ext
return managed.ExternalCreation{}, fmt.Errorf("could not apply cluster manifests: %w", err)
}
}
meta.SetExternalName(managedCluster, akuityAPICluster.Name)

return managed.ExternalCreation{}, err
}
Expand Down
11 changes: 9 additions & 2 deletions internal/controller/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"testing"

health "github.com/akuity/api-client-go/pkg/api/gen/types/status/health/v1"
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
Expand All @@ -33,6 +32,8 @@ import (
"k8s.io/kubectl/pkg/scheme"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

health "github.com/akuity/api-client-go/pkg/api/gen/types/status/health/v1"

"github.com/akuityio/provider-crossplane-akuity/apis/core/v1alpha1"
mock_akuity_client "github.com/akuityio/provider-crossplane-akuity/internal/clients/akuity/mock"
"github.com/akuityio/provider-crossplane-akuity/internal/controller/cluster"
Expand Down Expand Up @@ -303,7 +304,13 @@ func TestObserve_EmptyExternalName(t *testing.T) {
mockAkuityClient := mock_akuity_client.NewMockClient(gomock.NewController(t))
client := cluster.NewExternal(mockAkuityClient, fake.NewClientBuilder().Build(), logging.NewNopLogger())

resp, err := client.Observe(ctx, &v1alpha1.Cluster{})
resp, err := client.Observe(ctx, &v1alpha1.Cluster{
Spec: v1alpha1.ClusterSpec{
ForProvider: v1alpha1.ClusterParameters{
InstanceID: "test",
},
},
})
require.NoError(t, err)
assert.Equal(t, managed.ExternalObservation{ResourceExists: false}, resp)
}
Expand Down
8 changes: 6 additions & 2 deletions internal/controller/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

argocdv1 "github.com/akuity/api-client-go/pkg/api/gen/argocd/v1"
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/connection"
"github.com/crossplane/crossplane-runtime/pkg/controller"
Expand All @@ -36,6 +35,8 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"

argocdv1 "github.com/akuity/api-client-go/pkg/api/gen/argocd/v1"

"github.com/akuityio/provider-crossplane-akuity/apis/core/v1alpha1"
apisv1alpha1 "github.com/akuityio/provider-crossplane-akuity/apis/v1alpha1"
"github.com/akuityio/provider-crossplane-akuity/internal/clients/akuity"
Expand Down Expand Up @@ -72,7 +73,9 @@ func Setup(mgr ctrl.Manager, o controller.Options) error {
managed.WithLogger(logger),
managed.WithPollInterval(o.PollInterval),
managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))),
managed.WithConnectionPublishers(cps...))
managed.WithConnectionPublishers(cps...),
managed.WithInitializers(),
)

return ctrl.NewControllerManagedBy(mgr).
Named(name).
Expand Down Expand Up @@ -194,6 +197,7 @@ func (c *External) Create(ctx context.Context, mg resource.Managed) (managed.Ext
}

err = c.client.ApplyInstance(ctx, request)
meta.SetExternalName(managedInstance, request.GetId())

return managed.ExternalCreation{}, err
}
Expand Down
Loading