Created
October 1, 2021 19:58
-
-
Save jicowan/d2d0bc82fd31d6125cf33b2b42b1e62b to your computer and use it in GitHub Desktop.
Create Fargate Profile from eks-controller CRD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"github.com/aws-controllers-k8s/eks-controller/apis/v1alpha1" | |
"github.com/aws/aws-sdk-go/aws" | |
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | |
"k8s.io/apimachinery/pkg/runtime" | |
//"encoding/json" | |
"flag" | |
"fmt" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
//"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | |
"k8s.io/apimachinery/pkg/runtime/schema" | |
"k8s.io/client-go/dynamic" | |
"k8s.io/client-go/tools/clientcmd" | |
"k8s.io/client-go/util/homedir" | |
"path/filepath" | |
) | |
func main() { | |
var kubeconfig *string | |
if home := homedir.HomeDir(); home != "" { | |
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file") | |
} else { | |
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file") | |
} | |
flag.Parse() | |
// use the current context in kubeconfig | |
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig) | |
if err != nil { | |
panic(err.Error()) | |
} | |
profileSelector := &v1alpha1.FargateProfileSelector{ | |
Namespace: aws.String("bar"), | |
} | |
fgSpec := v1alpha1.FargateProfileSpec{ | |
ClusterName: aws.String("bottlerocket"), | |
Name: aws.String("fargate-bar"), | |
PodExecutionRoleARN: aws.String("arn:aws:iam::123456789012:role/AmazonEKSFargatePodExecutionRole"), | |
Selectors: []*v1alpha1.FargateProfileSelector{profileSelector}, | |
Subnets: aws.StringSlice([]string{"subnet-0b3633ef148863c83"}), | |
} | |
fargateInstance := &v1alpha1.FargateProfile{ | |
TypeMeta: metav1.TypeMeta{ | |
Kind: "FargateProfile", | |
APIVersion: "eks.services.k8s.aws/v1alpha1", | |
}, | |
ObjectMeta: metav1.ObjectMeta{ | |
Name: "fargate-bar", | |
}, | |
Spec: fgSpec, | |
Status: v1alpha1.FargateProfileStatus{}, | |
} | |
client, err := dynamic.NewForConfig(config) | |
output, err := client.Resource(schema.GroupVersionResource{ | |
Group: "eks.services.k8s.aws", | |
Version: "v1alpha1", | |
Resource: "fargateprofiles", | |
}).Namespace("default").Get(context.TODO(),"foo", metav1.GetOptions{}) | |
if err != nil { | |
fmt.Println(err) | |
} | |
fmt.Println(output) | |
obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(fargateInstance) | |
if err != nil { | |
panic(err) | |
} | |
u := &unstructured.Unstructured{Object: obj} | |
// create GVR. This should exist somewhere already | |
gvr := v1alpha1.GroupVersion.WithResource("fargateprofiles") | |
result := &v1alpha1.FargateProfile{} | |
n, err := client.Resource(gvr).Namespace("default").Create(context.TODO(), u, metav1.CreateOptions{}) | |
if err != nil { | |
panic(err) | |
} | |
runtime.DefaultUnstructuredConverter.FromUnstructured(n.Object, result) | |
fmt.Println(result) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment