Skip to content

Commit

Permalink
Add machinepool object, bump libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
mproffitt committed Nov 1, 2023
1 parent 62da4b8 commit 1f4d075
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
function-describe-nodegroups
crossplane-fn-describe-nodegroups

package/*.xpkg
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

Expand Down
53 changes: 49 additions & 4 deletions awsapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/errors"
xfnaws "github.com/giantswarm/xfnlib/pkg/auth/aws"
"github.com/giantswarm/xfnlib/pkg/composite"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
infrav2 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
expinfrav2 "sigs.k8s.io/cluster-api-provider-aws/v2/exp/api/v1beta2"
capiinfra "sigs.k8s.io/cluster-api/api/v1beta1"
)

// EKSNodegroupAPI describes the AWS functions required by this composition function
Expand Down Expand Up @@ -112,7 +114,7 @@ func (f *Function) CreateAWSNodegroupSpec(cluster, namespace, region, providerCo
}

var nodegroupName string = fmt.Sprintf("%s-%s", *cluster, nodegroup)
var awsmmp *expinfrav2.AWSManagedMachinePool = &expinfrav2.AWSManagedMachinePool{
var awsmmp expinfrav2.AWSManagedMachinePool = expinfrav2.AWSManagedMachinePool{
TypeMeta: metav1.TypeMeta{
Kind: "AWSManagedMachinePool",
APIVersion: "infrastructure.cluster.x-k8s.io/v1beta2",
Expand All @@ -132,14 +134,57 @@ func (f *Function) CreateAWSNodegroupSpec(cluster, namespace, region, providerCo
},
}

var object *unstructured.Unstructured
if object, err = composite.ToUnstructuredKubernetesObject(awsmmp, f.composite.Spec.ClusterProviderConfigRef); err != nil {
var dataSecretName string = ""
var machinepool *capiinfra.MachineDeployment = &capiinfra.MachineDeployment{
TypeMeta: metav1.TypeMeta{
Kind: "MachinePool",
APIVersion: "cluster.x-k8s.io/v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Name: nodegroupName + "-mp",
Namespace: *namespace,
Labels: labels,
Annotations: annotations,
},
Spec: capiinfra.MachineDeploymentSpec{
Replicas: &awsmmp.Status.Replicas,
ClusterName: *cluster,
Template: capiinfra.MachineTemplateSpec{
Spec: capiinfra.MachineSpec{
ClusterName: *cluster,
Bootstrap: capiinfra.Bootstrap{
DataSecretName: &dataSecretName,
},
InfrastructureRef: v1.ObjectReference{
Kind: "AWSManagedMachinePool",
APIVersion: "infrastructure.cluster.x-k8s.io/v1beta2",
Namespace: *namespace,
Name: nodegroupName,
},
},
},
},
}

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)
continue
}

f.log.Info("Adding nodegroup to required resources", "nodegroup", nodegroupName)
if err = f.composed.AddDesired(nodegroupName, object); err != nil {
if err = f.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)
continue
}

f.log.Info("Adding machinepool to required resources", "machinepool", nodegroupName)
if err = f.composed.AddDesired(nodegroupName+"-mp", mpobject); err != nil {
f.log.Info(composedName, "add machinepool", errors.Wrap(err, "cannot add composed object "+nodegroupName))
continue
}
Expand Down
10 changes: 7 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/bin/bash
go generate ./...
docker build . -t docker.io/choclab/function-describe-nodegroups:v0.0.1
docker push choclab/function-describe-nodegroups:v0.0.1
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
}
6 changes: 6 additions & 0 deletions fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
return rsp, nil
}

f.log.Info("input spec", "input", input)
if input.Spec == nil {
response.Normal(rsp, "Waiting for spec")
return rsp, nil
}

if _, ok := f.composed.ObservedComposed[input.Spec.ClusterRef]; !ok {
response.Normal(rsp, "Waiting for resource")
return rsp, nil
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ 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-20231030131937-61144db1085a
github.com/giantswarm/xfnlib v0.0.0-20231101162043-510e773708f8
github.com/google/go-cmp v0.6.0
google.golang.org/protobuf v1.31.0
k8s.io/api v0.28.3
k8s.io/apimachinery v0.28.3
k8s.io/client-go v0.28.3
sigs.k8s.io/cluster-api v1.5.2
sigs.k8s.io/cluster-api-provider-aws/v2 v2.2.4
sigs.k8s.io/controller-runtime v0.16.3
sigs.k8s.io/controller-tools v0.13.0
Expand Down Expand Up @@ -96,13 +98,11 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.28.3 // indirect
k8s.io/apiextensions-apiserver v0.28.3 // indirect
k8s.io/component-base v0.28.3 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
k8s.io/utils v0.0.0-20230505201702-9f6742963106 // indirect
sigs.k8s.io/cluster-api v1.5.2 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
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-20231030131937-61144db1085a h1:+PlWk1qSpyyCbtHmSFWjq2+5vTr9m5cDpeqfwsc9u1I=
github.com/giantswarm/xfnlib v0.0.0-20231030131937-61144db1085a/go.mod h1:FDbjZukEb2E0hdeHHgmCo8BP857+RBTMbVQ66JoMt/E=
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/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
2 changes: 1 addition & 1 deletion package/crossplane.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
apiVersion: meta.pkg.crossplane.io/v1beta1
kind: Function
metadata:
name: function-generate-subnets
name: function-describe-nodegroups
spec:
dependsOn:
- version: ">=v0.5.0"
Expand Down

0 comments on commit 1f4d075

Please sign in to comment.