Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
haarchri committed Jul 23, 2023
0 parents commit 483b123
Show file tree
Hide file tree
Showing 15 changed files with 1,405 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# crossplane xfn-nodegroup-loop

This Crossplane composition function relies on the CompositeResourceDefinition
of `XNodeGroup`

```
kubectl apply -f api/
kubectl apply -f example/
```

### Test locally.
```bash
cat test-input.yaml | go run .
```

### Build and push the image.
```bash
cd xfn
docker build \
--tag docker.io/haarchri/xfn-nodegroup-loop:v0.5.0 \
--push .
```
15 changes: 15 additions & 0 deletions api/composition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: xnodegroups.aws.haarchri.io
spec:
writeConnectionSecretsToNamespace: upbound-system
compositeTypeRef:
apiVersion: aws.haarchri.io/v1alpha1
kind: XNodeGroups
resources: []
functions:
- name: xfn-nodegroup-loop
type: Container
container:
image: haarchri/xfn-nodegroup-loop:v0.6.0
75 changes: 75 additions & 0 deletions api/definition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
apiVersion: apiextensions.crossplane.io/v1
kind: CompositeResourceDefinition
metadata:
name: xnodegroups.aws.haarchri.io
spec:
group: aws.haarchri.io
names:
kind: XNodeGroups
plural: xnodegroups
versions:
- name: v1alpha1
served: true
referenceable: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
description: NodeGroup configuration parameters.
properties:
subnetIds:
description: Identifiers of EC2 Subnets to associate with the
EKS Node Group. Amazon EKS managed node groups can be launched
in both public and private subnets. If you plan to deploy load
balancers to a subnet, the private subnet must have tag kubernetes.io/role/internal-elb,
the public subnet must have tag kubernetes.io/role/elb.
items:
type: string
type: array
region:
type: string
clusterName:
description: 100 characters in length. Must begin with an alphanumeric
character, and must only contain alphanumeric characters, dashes
and underscores (^[0-9A-Za-z][A-Za-z0-9\-_]+$).
type: string
nodeRoleArn:
description: Amazon Resource Name (ARN) of the IAM Role that
provides permissions for the EKS Node Group.
type: string
nodeGroups:
type: array
description: A list of NodeGroups that will created to a cluster.
items:
type: object
properties:
name:
description: Resource Name for EKS NodeGroup
type: string
instanceTypes:
description: List of instance types associated with the EKS Node
items:
type: string
type: array
scalingConfig:
description: Configuration block with scaling settings. Detailed
below.
items:
properties:
desiredSize:
description: Desired number of worker nodes.
type: number
maxSize:
description: Maximum number of worker nodes.
type: number
minSize:
description: Minimum number of worker nodes.
type: number
required:
- desiredSize
- maxSize
- minSize
type: object
type: array
26 changes: 26 additions & 0 deletions example/xnodegroup.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: aws.haarchri.io/v1alpha1
kind: XNodeGroups
metadata:
name: sample
spec:
region: eu-central-1
clusterName: platform-ref-aws3-5v9gj-pgm5m
nodeRoleArn: arn:aws:iam::123456789101:role/platform-ref-aws3-5v9gj-5b95t
subnetIds:
- subnet-01b461ac67338c5b0
- subnet-0a59082b6f593a290
nodeGroups:
- name: worker-nodes
instanceTypes:
- m5a.large
scalingConfig:
- minSize: 2
desiredSize: 3
maxSize: 5
- name: gpu-nodes
instanceTypes:
- g5a.large
scalingConfig:
- minSize: 2
desiredSize: 3
maxSize: 5
21 changes: 21 additions & 0 deletions xfn/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM golang:1.20-alpine3.17 as builder

ARG TARGETOS
ARG TARGETARCH

WORKDIR /app

COPY . .
RUN go mod download

RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o /function .

###
FROM alpine:3.17.3

LABEL org.opencontainers.image.source="https://github.com/haarchri/xfn-nodegroup-loop"

COPY crossplane.yaml /crossplane.yaml
COPY --from=builder /function /function

ENTRYPOINT ["/function"]
36 changes: 36 additions & 0 deletions xfn/aws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
v1beta1 "github.com/upbound/provider-aws/apis/eks/v1beta1"
)

func WrapForAWS(parameters ResourceParameters, instanceType []*string, scalingConfigs []ScalingConfigParameters) *v1beta1.NodeGroup {
o := &v1beta1.NodeGroup{
Spec: v1beta1.NodeGroupSpec{
ForProvider: v1beta1.NodeGroupParameters{
InstanceTypes: instanceType,
Region: parameters.Region,
ClusterName: parameters.ClusterName,
NodeRoleArn: parameters.NodeRoleArn,
},
// ResourceSpec: v1.ResourceSpec{
// ProviderConfigReference: &v1.Reference{
// Name: providerConfigName,
// },
// },
},
}

for _, subnetId := range parameters.SubnetIds {
o.Spec.ForProvider.SubnetIds = append(o.Spec.ForProvider.SubnetIds, subnetId)
}
for _, scalingConfig := range scalingConfigs {
o.Spec.ForProvider.ScalingConfig = append(o.Spec.ForProvider.ScalingConfig, v1beta1.ScalingConfigParameters{
DesiredSize: scalingConfig.DesiredSize,
MinSize: scalingConfig.MinSize,
MaxSize: scalingConfig.MaxSize,
})
}
o.SetGroupVersionKind(v1beta1.NodeGroup_GroupVersionKind)
return o
}
36 changes: 36 additions & 0 deletions xfn/composite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
)

func GetNodeGroupEntries(cp *fieldpath.Paved) ([]NodeGroupsEntry, error) {
nodeGroup := []NodeGroupsEntry{}
if err := cp.GetValueInto("spec.nodeGroups", &nodeGroup); err != nil {
return nil, errors.Wrap(err, "failed to get spec.nodeGroups from observed composite")
}

return nodeGroup, nil
}

func GetResourceEntries(cp *fieldpath.Paved) (*ResourceParameters, error) {
paramters := ResourceParameters{}
if err := cp.GetValueInto("spec.region", &paramters.Region); err != nil {
return nil, errors.Wrap(err, "failed to get spec.region from observed composite")
}

if err := cp.GetValueInto("spec.clusterName", &paramters.ClusterName); err != nil {
return nil, errors.Wrap(err, "failed to get spec.clusterName from observed composite")
}

if err := cp.GetValueInto("spec.subnetIds", &paramters.SubnetIds); err != nil {
return nil, errors.Wrap(err, "failed to get spec.subnetIds from observed composite")
}

if err := cp.GetValueInto("spec.nodeRoleArn", &paramters.NodeRoleArn); err != nil {
return nil, errors.Wrap(err, "failed to get spec.nodeRoleArn from observed composite")
}

return &paramters, nil
}
10 changes: 10 additions & 0 deletions xfn/crossplane.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: meta.pkg.crossplane.io/v1alpha1
kind: Function
metadata:
name: xfn-nodegroup-loop
annotations:
meta.crossplane.io/maintainer: haarchri
meta.crossplane.io/source: github.com/haarchri/xfn-nodegroup-loop
meta.crossplane.io/license: Apache-2.0
meta.crossplane.io/description: |
A Composition Function that creates provider AWS NodeGroups for every given entry.
102 changes: 102 additions & 0 deletions xfn/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
module github.com/haarchri/xfn-nodegroup-loop

go 1.20

require (
github.com/crossplane/crossplane v1.11.3
github.com/crossplane/crossplane-runtime v0.20.0-rc.0.0.20230406155702-4e1673b7141f
k8s.io/apimachinery v0.27.3
sigs.k8s.io/yaml v1.3.0
)

require (
github.com/agext/levenshtein v1.2.3 // indirect
github.com/antchfx/htmlquery v1.2.4 // indirect
github.com/antchfx/xpath v1.2.0 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/fatih/camelcase v1.0.0 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
github.com/hashicorp/go-hclog v1.2.1 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/hcl/v2 v2.14.1 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-json v0.14.0 // indirect
github.com/hashicorp/terraform-plugin-go v0.14.0 // indirect
github.com/hashicorp/terraform-plugin-log v0.7.0 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.0 // indirect
github.com/iancoleman/strcase v0.2.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/prometheus/client_golang v1.15.1 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.10.0 // indirect
github.com/tmccombs/hcl2json v0.3.3 // indirect
github.com/upbound/upjet v0.9.0-rc.0.0.20230502154751-645d7260d814 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
github.com/vmihailenco/tagparser v0.1.1 // indirect
github.com/yuin/goldmark v1.4.13 // indirect
github.com/zclconf/go-cty v1.11.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.3.0 // indirect
k8s.io/component-base v0.27.3 // indirect
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.10.2 // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/upbound/provider-aws v0.34.0
golang.org/x/net v0.12.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
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.27.3 // indirect
k8s.io/client-go v0.27.3 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230525220651-2546d827e515 // indirect
k8s.io/utils v0.0.0-20230505201702-9f6742963106 // indirect
sigs.k8s.io/controller-runtime v0.15.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)
Loading

0 comments on commit 483b123

Please sign in to comment.