Skip to content

Commit

Permalink
Fix for baked in cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mproffitt committed Nov 9, 2023
1 parent e823ff3 commit 5f6d843
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 55 deletions.
48 changes: 24 additions & 24 deletions awsapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ func GetAutoScalingGroups(c context.Context, api AutoscalingAPI, input *asg.Desc
return api.DescribeAutoScalingGroups(c, input)
}

func (f *Function) CreateAWSNodegroupSpec(cluster, namespace, region, providerConfig *string, labels, annotations map[string]string) (err error) {
func (f *Function) CreateAWSNodegroupSpec(ac *awsconfig) (err error) {
var (
res *eks.ListNodegroupsOutput
cfg aws.Config
)

// Set up the assume role clients
if cfg, err = xfnaws.Config(region, providerConfig); err != nil {
if cfg, err = xfnaws.Config(ac.region, ac.providerConfigRef); err != nil {
err = errors.Wrap(err, "failed to load aws config for assume role")
return
}
Expand All @@ -88,42 +88,42 @@ func (f *Function) CreateAWSNodegroupSpec(cluster, namespace, region, providerCo
// end setting up clients

clusterInput := &eks.ListNodegroupsInput{
ClusterName: cluster,
ClusterName: ac.cluster,
}

if res, err = GetNodegroups(context.TODO(), eksclient, clusterInput); err != nil {
err = errors.Wrap(err, fmt.Sprintf("failed to load nodegroups for cluster %q", *cluster))
err = errors.Wrap(err, fmt.Sprintf("failed to load nodegroups for cluster %q", *ac.cluster))
return
}

for _, nodegroup := range res.Nodegroups {
nodegroupInput := &eks.DescribeNodegroupInput{
ClusterName: cluster,
ClusterName: ac.cluster,
NodegroupName: &nodegroup,
}
var group *eks.DescribeNodegroupOutput
if group, err = DescribeNodegroup(context.TODO(), eksclient, nodegroupInput); err != nil {
f.log.Debug(fmt.Sprintf("cannot describe nodegroup %s for cluster %s", nodegroup, *cluster), "error was", err)
f.log.Debug(fmt.Sprintf("cannot describe nodegroup %s for cluster %s", nodegroup, *ac.cluster), "error was", err)
continue
}

var ng *expinfrav2.AWSManagedMachinePoolSpec
if ng, err = f.nodegroupToCapiObject(group.Nodegroup, ec2client, asgclient); err != nil {
f.log.Debug(fmt.Sprintf("cannot create nodegroup object for nodegroup %q in cluster %q", nodegroup, *cluster), "error was", err)
f.log.Debug(fmt.Sprintf("cannot create nodegroup object for nodegroup %q in cluster %q", nodegroup, *ac.cluster), "error was", err)
continue
}

var nodegroupName string = fmt.Sprintf("%s-%s", *cluster, nodegroup)
var nodegroupName string = fmt.Sprintf("%s-%s", *ac.cluster, nodegroup)
var awsmmp expinfrav2.AWSManagedMachinePool = expinfrav2.AWSManagedMachinePool{
TypeMeta: metav1.TypeMeta{
Kind: "AWSManagedMachinePool",
APIVersion: "infrastructure.cluster.x-k8s.io/v1beta2",
},
ObjectMeta: metav1.ObjectMeta{
Name: nodegroupName,
Namespace: *namespace,
Labels: labels,
Annotations: annotations,
Namespace: *ac.namespace,
Labels: ac.labels,
Annotations: ac.annotations,
},
Spec: *ng,
Status: expinfrav2.AWSManagedMachinePoolStatus{
Expand All @@ -142,23 +142,23 @@ func (f *Function) CreateAWSNodegroupSpec(cluster, namespace, region, providerCo
},
ObjectMeta: metav1.ObjectMeta{
Name: nodegroupName + "-mp",
Namespace: *namespace,
Labels: labels,
Annotations: annotations,
Namespace: *ac.namespace,
Labels: ac.labels,
Annotations: ac.annotations,
},
Spec: capiinfra.MachineDeploymentSpec{
Replicas: &awsmmp.Status.Replicas,
ClusterName: *cluster,
ClusterName: *ac.cluster,
Template: capiinfra.MachineTemplateSpec{
Spec: capiinfra.MachineSpec{
ClusterName: *cluster,
ClusterName: *ac.cluster,
Bootstrap: capiinfra.Bootstrap{
DataSecretName: &dataSecretName,
},
InfrastructureRef: v1.ObjectReference{
Kind: "AWSManagedMachinePool",
APIVersion: "infrastructure.cluster.x-k8s.io/v1beta2",
Namespace: *namespace,
Namespace: *ac.namespace,
Name: nodegroupName,
},
},
Expand All @@ -167,24 +167,24 @@ func (f *Function) CreateAWSNodegroupSpec(cluster, namespace, region, providerCo
}

var awsobject, mpobject *unstructured.Unstructured
if awsobject, err = composite.ToUnstructuredKubernetesObject(awsmmp, f.composite.Spec.ClusterProviderConfigRef); err != nil {
f.log.Debug(fmt.Sprintf("failed to convert nodegroup %q to kubernetes object for cluster %q.", nodegroup, *cluster), "error was", err)
if awsobject, err = composite.ToUnstructuredKubernetesObject(awsmmp, ac.composite.Spec.ClusterProviderConfigRef); err != nil {
f.log.Debug(fmt.Sprintf("failed to convert nodegroup %q to kubernetes object for cluster %q.", nodegroup, *ac.cluster), "error was", err)
continue
}

f.log.Info("Adding nodegroup to required resources", "nodegroup", nodegroupName)
if err = f.composed.AddDesired(nodegroupName, awsobject); err != nil {
if err = ac.composed.AddDesired(nodegroupName, awsobject); err != nil {
f.log.Info(composedName, "add machinepool", errors.Wrap(err, "cannot add composed object "+nodegroupName))
continue
}

if mpobject, err = composite.ToUnstructuredKubernetesObject(machinepool, f.composite.Spec.ClusterProviderConfigRef); err != nil {
f.log.Debug(fmt.Sprintf("failed to convert nodegroup %q to kubernetes object for cluster %q.", nodegroup, *cluster), "error was", err)
if mpobject, err = composite.ToUnstructuredKubernetesObject(machinepool, ac.composite.Spec.ClusterProviderConfigRef); err != nil {
f.log.Debug(fmt.Sprintf("failed to convert nodegroup %q to kubernetes object for cluster %q.", nodegroup, *ac.cluster), "error was", err)
continue
}

f.log.Info("Adding machinepool to required resources", "machinepool", nodegroupName)
if err = f.composed.AddDesired(nodegroupName+"-mp", mpobject); err != nil {
if err = ac.composed.AddDesired(nodegroupName+"-mp", mpobject); err != nil {
f.log.Info(composedName, "add machinepool", errors.Wrap(err, "cannot add composed object "+nodegroupName))
continue
}
Expand Down Expand Up @@ -218,7 +218,7 @@ func (f *Function) nodegroupToCapiObject(group *types.Nodegroup, ec2client *ec2.
}
}

pool.AMIVersion = group.Version
// pool.AMIVersion = group.Version

pool.AvailabilityZones = autoscaling.AvailabilityZones

Expand Down
10 changes: 6 additions & 4 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/bin/bash

VERSION=v0.0.4
go build . && {
rm package/*.xpkg
go generate ./...
docker buildx build . -t docker.io/choclab/function-describe-nodegroups:v0.0.1
crossplane xpkg build -f package --embed-runtime-image=docker.io/choclab/function-describe-nodegroups:v0.0.1
crossplane xpkg push -f package/$(ls package | grep function-describe) docker.io/choclab/function-describe-nodegroups:v0.0.1
}
docker buildx build . -t docker.io/choclab/function-describe-nodegroups:${VERSION}
crossplane xpkg build -f package --embed-runtime-image=docker.io/choclab/function-describe-nodegroups:${VERSION}
crossplane xpkg push -f package/$(ls package | grep function-describe) docker.io/choclab/function-describe-nodegroups:${VERSION}
}
43 changes: 22 additions & 21 deletions fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ const composedName = "crossplane-fn-describe-nodegroups"
// RunFunction Execute the desired reconcilliation state, creating any required resources
func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequest) (rsp *fnv1beta1.RunFunctionResponse, err error) {
f.log.Info("preparing function", composedName, req.GetMeta().GetTag())

rsp = response.To(req, response.DefaultTTL)

input := v1beta1.Input{}
if f.composed, err = composite.New(req, &input, &f.composite); err != nil {
var (
ac awsconfig = awsconfig{}
input v1beta1.Input
)
if ac.composed, err = composite.New(req, &input, &ac.composite); err != nil {
response.Fatal(rsp, errors.Wrap(err, "error setting up function "+composedName))
return rsp, nil
}
Expand All @@ -30,34 +34,31 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
return rsp, nil
}

if _, ok := f.composed.ObservedComposed[input.Spec.ClusterRef]; !ok {
if _, ok := ac.composed.ObservedComposed[input.Spec.ClusterRef]; !ok {
response.Normal(rsp, "Waiting for resource")
return rsp, nil
}

var (
clusterName *string = &f.composite.Spec.ClusterName
namespace *string = &f.composite.Spec.ClaimRef.Namespace
region *string = &f.composite.Spec.Region
provider *string = &f.composite.Spec.CompositionSelector.MatchLabels.Provider
providerConfigRef *string = &f.composite.Spec.CloudProviderConfigRef

labels map[string]string = f.composite.Metadata.Labels
annotations map[string]string = map[string]string{
"cluster.x-k8s.io/managed-by": "crossplane",
}
)
ac.cluster = &ac.composite.Spec.ClusterName
ac.namespace = &ac.composite.Spec.ClaimRef.Namespace
ac.region = &ac.composite.Spec.Region
ac.providerConfigRef = &ac.composite.Spec.CloudProviderConfigRef

ac.annotations = map[string]string{
"cluster.x-k8s.io/managed-by": "crossplane",
}
ac.labels = ac.composite.Metadata.Labels
// Merge in the additional labels for kubernetes resources
for k, v := range f.composite.Spec.KubernetesAdditionalLabels {
labels[k] = v
for k, v := range ac.composite.Spec.KubernetesAdditionalLabels {
ac.labels[k] = v
}

f.log.Info(*provider)
switch strings.ToLower(*provider) {
var provider string = ac.composite.Spec.CompositionSelector.MatchLabels.Provider
f.log.Info(provider)
switch strings.ToLower(provider) {
case "aws":
f.log.Info("discovered aws provider", composedName, req.GetMeta().GetTag())
if err = f.CreateAWSNodegroupSpec(clusterName, namespace, region, providerConfigRef, labels, annotations); err != nil {
if err = f.CreateAWSNodegroupSpec(&ac); err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot get desired composite resources from %T", req))
return rsp, nil
}
Expand All @@ -67,7 +68,7 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
f.log.Info("GCP provider is not yet implemented")
}

if err = f.composed.ToResponse(rsp); err != nil {
if err = ac.composed.ToResponse(rsp); err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot convert composition to response %T", rsp))
return
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/crossplane/crossplane-runtime v1.14.0-rc.1
github.com/crossplane/function-sdk-go v0.0.0-20231027134439-0745c2a72577
github.com/crossplane/function-template-go v0.0.0-20230930023403-40dc198e7a6c
github.com/giantswarm/xfnlib v0.0.0-20231101162043-510e773708f8
github.com/giantswarm/xfnlib v0.0.0-20231109102154-99e33378d85b
github.com/google/go-cmp v0.6.0
google.golang.org/protobuf v1.31.0
k8s.io/api v0.28.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/giantswarm/xfnlib v0.0.0-20231101162043-510e773708f8 h1:EuZoljt79iTSGRL5ubIcve8iCrrCjXTra9cn15R/m9M=
github.com/giantswarm/xfnlib v0.0.0-20231101162043-510e773708f8/go.mod h1:JliA7y2iygDQPpaU1f3wzqi5q3KP5bWKjVQpt0+D/z4=
github.com/giantswarm/xfnlib v0.0.0-20231109102154-99e33378d85b h1:HOyzkpqT/XQL8/6CTSHxc8Vwo33Oxr9EAnkjO65J73Y=
github.com/giantswarm/xfnlib v0.0.0-20231109102154-99e33378d85b/go.mod h1:JliA7y2iygDQPpaU1f3wzqi5q3KP5bWKjVQpt0+D/z4=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
Expand Down
11 changes: 8 additions & 3 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,15 @@ type XRStatus struct {
AWSRoleArn string `json:"roleArn"`
}

type awsconfig struct {
cluster, namespace, region, providerConfigRef *string
labels, annotations map[string]string
composed *composite.Composition
composite EksImportXRObject
}

// Function returns whatever response you ask it to.
type Function struct {
fnv1beta1.UnimplementedFunctionRunnerServiceServer
log logging.Logger
composed *composite.Composition
composite EksImportXRObject
log logging.Logger
}

0 comments on commit 5f6d843

Please sign in to comment.