Skip to content

Commit 13a6161

Browse files
authored
feat: group controller uses cloudian sdk (#57)
1 parent 0ce3c93 commit 13a6161

File tree

3 files changed

+59
-11
lines changed

3 files changed

+59
-11
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
gopkg.in/alecthomas/kingpin.v2 v2.2.6
1111
k8s.io/apimachinery v0.31.3
1212
k8s.io/client-go v0.31.3
13+
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078
1314
sigs.k8s.io/controller-runtime v0.19.2
1415
sigs.k8s.io/controller-tools v0.16.5
1516
)
@@ -86,7 +87,6 @@ require (
8687
k8s.io/component-base v0.31.3 // indirect
8788
k8s.io/klog/v2 v2.130.1 // indirect
8889
k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f // indirect
89-
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078 // indirect
9090
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect
9191
sigs.k8s.io/structured-merge-diff/v4 v4.4.3 // indirect
9292
sigs.k8s.io/yaml v1.4.0 // indirect

internal/controller/group/group.go

+51-10
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ package group
1818

1919
import (
2020
"context"
21-
"fmt"
2221

2322
"github.com/pkg/errors"
2423
"k8s.io/apimachinery/pkg/types"
24+
"k8s.io/utils/ptr"
2525
ctrl "sigs.k8s.io/controller-runtime"
2626
"sigs.k8s.io/controller-runtime/pkg/client"
2727

28+
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
2829
"github.com/crossplane/crossplane-runtime/pkg/connection"
2930
"github.com/crossplane/crossplane-runtime/pkg/controller"
3031
"github.com/crossplane/crossplane-runtime/pkg/event"
@@ -44,7 +45,11 @@ const (
4445
errGetPC = "cannot get ProviderConfig"
4546
errGetCreds = "cannot get credentials"
4647

47-
errNewClient = "cannot create new Service"
48+
errNewClient = "cannot create new Service"
49+
errCreateGroup = "cannot create Group"
50+
errDeleteGroup = "cannot delete Group"
51+
errGetGroup = "cannot get Group"
52+
errUpdateGroup = "cannot update Group"
4853
)
4954

5055
// A NoOpService does nothing.
@@ -145,8 +150,15 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
145150
return managed.ExternalObservation{}, errors.New(errNotGroup)
146151
}
147152

148-
// These fmt statements should be removed in the real implementation.
149-
fmt.Printf("Observing: %+v", cr)
153+
observedGroup, err := c.cloudianService.GetGroup(ctx, cr.Spec.ForProvider.GroupID)
154+
if err != nil {
155+
if errors.Is(err, cloudian.ErrNotFound) {
156+
return managed.ExternalObservation{ResourceExists: false}, nil
157+
}
158+
return managed.ExternalObservation{}, errors.Wrap(err, errGetGroup)
159+
}
160+
161+
cr.SetConditions(xpv1.Available())
150162

151163
return managed.ExternalObservation{
152164
// Return false when the external resource does not exist. This lets
@@ -157,7 +169,7 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
157169
// Return false when the external resource exists, but it not up to date
158170
// with the desired managed resource state. This lets the managed
159171
// resource reconciler know that it needs to call Update.
160-
ResourceUpToDate: true,
172+
ResourceUpToDate: isUpToDate(cr.Spec.ForProvider, *observedGroup),
161173

162174
// Return any details that may be required to connect to the external
163175
// resource. These will be stored as the connection secret.
@@ -171,7 +183,11 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext
171183
return managed.ExternalCreation{}, errors.New(errNotGroup)
172184
}
173185

174-
fmt.Printf("Creating: %+v", cr)
186+
cr.SetConditions(xpv1.Creating())
187+
188+
if err := c.cloudianService.CreateGroup(ctx, newGroupFromParams(cr.Spec.ForProvider)); err != nil {
189+
return managed.ExternalCreation{}, errors.Wrap(err, errCreateGroup)
190+
}
175191

176192
return managed.ExternalCreation{
177193
// Optionally return any details that may be required to connect to the
@@ -186,7 +202,9 @@ func (c *external) Update(ctx context.Context, mg resource.Managed) (managed.Ext
186202
return managed.ExternalUpdate{}, errors.New(errNotGroup)
187203
}
188204

189-
fmt.Printf("Updating: %+v", cr)
205+
if err := c.cloudianService.UpdateGroup(ctx, newGroupFromParams(cr.Spec.ForProvider)); err != nil {
206+
return managed.ExternalUpdate{}, errors.Wrap(err, errUpdateGroup)
207+
}
190208

191209
return managed.ExternalUpdate{
192210
// Optionally return any details that may be required to connect to the
@@ -201,12 +219,35 @@ func (c *external) Delete(ctx context.Context, mg resource.Managed) (managed.Ext
201219
return managed.ExternalDelete{}, errors.New(errNotGroup)
202220
}
203221

204-
fmt.Printf("Deleting: %+v", cr)
222+
cr.SetConditions(xpv1.Deleting())
223+
224+
if err := c.cloudianService.DeleteGroup(ctx, cr.Spec.ForProvider.GroupID); err != nil {
225+
return managed.ExternalDelete{}, errors.Wrap(err, errDeleteGroup)
226+
}
205227

206228
return managed.ExternalDelete{}, nil
207229
}
208230

209231
func (c *external) Disconnect(ctx context.Context) error {
210-
// TODO implement me
211-
panic("implement me")
232+
return nil
233+
}
234+
235+
func isUpToDate(desired v1alpha1.GroupParameters, observed cloudian.Group) bool {
236+
return newGroupFromParams(desired) == observed
237+
}
238+
239+
func newGroupFromParams(gp v1alpha1.GroupParameters) cloudian.Group {
240+
defaultsGroup := cloudian.NewGroup(gp.GroupID)
241+
return cloudian.Group{
242+
Active: gp.Active,
243+
GroupID: gp.GroupID,
244+
GroupName: gp.GroupName,
245+
LDAPEnabled: ptr.Deref(gp.LDAPEnabled, defaultsGroup.LDAPEnabled),
246+
LDAPGroup: ptr.Deref(gp.LDAPGroup, defaultsGroup.LDAPGroup),
247+
LDAPMatchAttribute: ptr.Deref(gp.LDAPMatchAttribute, defaultsGroup.LDAPMatchAttribute),
248+
LDAPSearch: ptr.Deref(gp.LDAPSearch, defaultsGroup.LDAPSearch),
249+
LDAPSearchUserBase: ptr.Deref(gp.LDAPSearchUserBase, defaultsGroup.LDAPSearchUserBase),
250+
LDAPServerURL: ptr.Deref(gp.LDAPServerURL, defaultsGroup.LDAPServerURL),
251+
LDAPUserDNTemplate: ptr.Deref(gp.LDAPUserDNTemplate, defaultsGroup.LDAPUserDNTemplate),
252+
}
212253
}

internal/sdk/cloudian/sdk.go

+7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ type groupInternal struct {
4848
S3WebSiteEndpoints []string `json:"s3websiteendpoints"`
4949
}
5050

51+
// A new group with cloudian defaults set
52+
func NewGroup(groupId string) Group {
53+
return Group{
54+
GroupID: groupId,
55+
}
56+
}
57+
5158
func toInternal(g Group) groupInternal {
5259
return groupInternal{
5360
Active: strconv.FormatBool(g.Active),

0 commit comments

Comments
 (0)