Skip to content

Instantly share code, notes, and snippets.

@leonsteinhaeuser
Created May 26, 2021 21:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonsteinhaeuser/c7ffee30911f09a6aa19bd3dd383cdd8 to your computer and use it in GitHub Desktop.
Save leonsteinhaeuser/c7ffee30911f09a6aa19bd3dd383cdd8 to your computer and use it in GitHub Desktop.
k8s watch custom resource
package main
import (
"context"
"fmt"
v1 "github.com/programmfabrik/k8s-fylr-operator/api/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic"
ctrl "sigs.k8s.io/controller-runtime"
)
func panicIfError(err error) {
if err != nil {
panic(err)
}
}
func main() {
config, err := ctrl.GetConfig()
panicIfError(err)
dynamicInterface, err := dynamic.NewForConfig(config)
panicIfError(err)
// defines a group version resource (gvr) that is used to identify objects within kubernetes.
// If the gvr could not be found within kubernetes, the string inside the WithResource function is probably false.
groupVersionResource := v1.SchemeBuilder.GroupVersion.WithResource("instances")
// watches the specified type
watchInterface, err := dynamicInterface.Resource(groupVersionResource).Watch(context.Background(), metav1.ListOptions{
Watch: true,
})
panicIfError(err)
// receive the channel containing the data consumed by the watcher
wch := watchInterface.ResultChan()
for {
dt := <-wch
// convert the runtime.Object to *unstructured.Unstructured
// it is not possible to convert this directly to v1.Instance because kubernetes returns an object of type unstructured.Unstructured
typeUnstructured := dt.Object.(*unstructured.Unstructured)
inst := &v1.Instance{}
// finally we can convert the object from the unstructured.Unstructured type to the concrete type
err := runtime.DefaultUnstructuredConverter.FromUnstructured(typeUnstructured.UnstructuredContent(), inst)
panicIfError(err)
// now we can use the instance that contains all the information we provided before
fmt.Printf("EventType: %v\nInstance:\n%+v\n\n", dt.Type, inst)
}
}
@leonsteinhaeuser
Copy link
Author

Example output:

CREATE

EventType: ADDED
Instance:
&{TypeMeta:{Kind:Instance APIVersion:app.fylr.io/v1} ObjectMeta:{Name:instance-sample GenerateName: Namespace:fylr-sample SelfLink:/apis/app.fylr.io/v1/namespaces/fylr-sample/instances/instance-sample UID:29dc00cb-a9ea-4864-b919-d3de23c15ed7 ResourceVersion:5025908 Generation:1 CreationTimestamp:2021-05-26 23:21:25 +0200 CEST DeletionTimestamp:<nil> DeletionGracePeriodSeconds:<nil> Labels:map[] Annotations:map[kubectl.kubernetes.io/last-applied-configuration:{"apiVersion":"app.fylr.io/v1","kind":"Instance","metadata":{"annotations":{},"name":"instance-sample","namespace":"fylr-sample"},"spec":{"fylr":{"allowPurge":true,"authentication":{"application":{"root":"admin"},"inspect":{"root":"admin"}},"database":{"databaseSecretName":"fylr-database-secret","maxIdleConns":10,"maxOpenConns":10},"elastic":{"elasticSecretName":"fylr-elastic-secret","maxMem":"100mb","objectsPerJob":100,"parallel":2},"execserver":{"addresses":["http://localhost:8082"],"callbackTimeout":30,"parallel":2,"type":"combined"},"externalURL":"example.local","s3":{"s3SecretName":"fylr-s3-secret"}},"ingress":{"bodySize":"1g","clusterIssuer":"letsencrypt-staging","proxyConnectTimeout":300,"proxyNextUpstreamTimeout":300,"proxyReadTimeout":300,"proxyRequestBuffering":"off","proxySendTimeout":300},"pod":{"pullPolicy":"Always","replicas":1,"resources":{"limits":{"cpu":"1000m","memory":"1024Mi"},"requests":{"cpu":"64m","memory":"32Mi"}},"strategy":{"rollingUpdate":{"maxSurge":1,"maxUnavailable":1},"type":"RollingUpdate"},"version":"develop"}}}
] OwnerReferences:[] Finalizers:[] ClusterName: ManagedFields:[{Manager:kubectl-client-side-apply Operation:Update APIVersion:app.fylr.io/v1 Time:2021-05-26 23:21:25 +0200 CEST FieldsType:FieldsV1 FieldsV1:{"f:metadata":{"f:annotations":{".":{},"f:kubectl.kubernetes.io/last-applied-configuration":{}}},"f:spec":{".":{},"f:fylr":{".":{},"f:allowPurge":{},"f:authentication":{".":{},"f:application":{".":{},"f:root":{}},"f:inspect":{".":{},"f:root":{}}},"f:database":{".":{},"f:databaseSecretName":{},"f:maxIdleConns":{},"f:maxOpenConns":{}},"f:elastic":{".":{},"f:elasticSecretName":{},"f:maxMem":{},"f:objectsPerJob":{},"f:parallel":{}},"f:execserver":{".":{},"f:addresses":{},"f:callbackTimeout":{},"f:parallel":{},"f:type":{}},"f:externalURL":{},"f:s3":{".":{},"f:s3SecretName":{}}},"f:ingress":{".":{},"f:bodySize":{},"f:clusterIssuer":{},"f:proxyConnectTimeout":{},"f:proxyNextUpstreamTimeout":{},"f:proxyReadTimeout":{},"f:proxyRequestBuffering":{},"f:proxySendTimeout":{}},"f:pod":{".":{},"f:pullPolicy":{},"f:replicas":{},"f:resources":{".":{},"f:limits":{".":{},"f:cpu":{},"f:memory":{}},"f:requests":{".":{},"f:cpu":{},"f:memory":{}}},"f:strategy":{".":{},"f:rollingUpdate":{".":{},"f:maxSurge":{},"f:maxUnavailable":{}},"f:type":{}},"f:version":{}}}}}]} Spec:{Pod:{Replicas:1 Version:develop NodeSelector:map[] Affinity:nil PullPolicy:Always Resources:{Limits:map[cpu:{i:{value:1000 scale:-3} d:{Dec:<nil>} s: Format:DecimalSI} memory:{i:{value:1073741824 scale:0} d:{Dec:<nil>} s: Format:BinarySI}] Requests:map[cpu:{i:{value:64 scale:-3} d:{Dec:<nil>} s:64m Format:DecimalSI} memory:{i:{value:33554432 scale:0} d:{Dec:<nil>} s: Format:BinarySI}]} Strategy:{Type:RollingUpdate RollingUpdate:&RollingUpdateDeployment{MaxUnavailable:1,MaxSurge:1,}}} Ingress:{ClusterIssuer:letsencrypt-staging BodySize:1g ProxyConnectTimeout:300 ProxySendTimeout:300 ProxyReadTimeout:300 ProxyNextUpstreamTimeout:300 ProxyRequestBuffering:off} Fylr:{ExternalURL:example.local AllowPurge:true Authentication:{Application:map[root:admin] Inspect:map[root:admin]} Database:{MaxOpenConns:10 MaxIdleConns:10 DatabaseSecretName:fylr-database-secret} Elastic:{Parallel:2 ObjectsPerJob:100 MaxMem:100mb ElasticSecretName:fylr-elastic-secret} S3:{S3SecretName:fylr-s3-secret} Execserver:{Type:combined Parallel:2 CallbackTimeout:30 Addresses:[http://localhost:8082]}}} Status:{Phase: Replicas: Database: ServiceDNSAddress: ServiceIPv4Address: LastMessages:}}

UPDATE

EventType: MODIFIED
Instance:
&{TypeMeta:{Kind:Instance APIVersion:app.fylr.io/v1} ObjectMeta:{Name:instance-sample GenerateName: Namespace:default SelfLink:/apis/app.fylr.io/v1/namespaces/default/instances/instance-sample UID:c2ac6aba-0f86-4856-af1f-1189911553be ResourceVersion:5025978 Generation:4 CreationTimestamp:2021-05-26 21:38:46 +0200 CEST DeletionTimestamp:<nil> DeletionGracePeriodSeconds:<nil> Labels:map[] Annotations:map[kubectl.kubernetes.io/last-applied-configuration:{"apiVersion":"app.fylr.io/v1","kind":"Instance","metadata":{"annotations":{},"name":"instance-sample","namespace":"default"},"spec":{"fylr":{"allowPurge":true,"authentication":{"application":{"root":"admin"},"inspect":{"root":"admin"}},"database":{"databaseSecretName":"fylr-database-secret","maxIdleConns":10,"maxOpenConns":10},"elastic":{"elasticSecretName":"fylr-elastic-secret","maxMem":"100mb","objectsPerJob":100,"parallel":2},"execserver":{"addresses":["http://localhost:8082"],"callbackTimeout":30,"parallel":2,"type":"combined"},"externalURL":"example.local","s3":{"s3SecretName":"fylr-s3-secret"}},"ingress":{"bodySize":"1g","clusterIssuer":"letsencrypt-staging","proxyConnectTimeout":300,"proxyNextUpstreamTimeout":300,"proxyReadTimeout":300,"proxyRequestBuffering":"off","proxySendTimeout":300},"pod":{"pullPolicy":"Always","replicas":1,"resources":{"limits":{"cpu":"1000m","memory":"1024Mi"},"requests":{"cpu":"64m","memory":"32Mi"}},"strategy":{"rollingUpdate":{"maxSurge":1,"maxUnavailable":1},"type":"RollingUpdate"},"version":"develop"}}}
] OwnerReferences:[] Finalizers:[] ClusterName: ManagedFields:[{Manager:kubectl-client-side-apply Operation:Update APIVersion:app.fylr.io/v1 Time:2021-05-26 21:38:46 +0200 CEST FieldsType:FieldsV1 FieldsV1:{"f:metadata":{"f:annotations":{".":{},"f:kubectl.kubernetes.io/last-applied-configuration":{}}},"f:spec":{".":{},"f:fylr":{".":{},"f:allowPurge":{},"f:authentication":{".":{},"f:application":{".":{},"f:root":{}},"f:inspect":{".":{},"f:root":{}}},"f:database":{".":{},"f:databaseSecretName":{},"f:maxIdleConns":{},"f:maxOpenConns":{}},"f:elastic":{".":{},"f:elasticSecretName":{},"f:maxMem":{},"f:objectsPerJob":{}},"f:execserver":{".":{},"f:addresses":{},"f:callbackTimeout":{},"f:type":{}},"f:externalURL":{},"f:s3":{".":{},"f:s3SecretName":{}}},"f:ingress":{".":{},"f:bodySize":{},"f:clusterIssuer":{},"f:proxyConnectTimeout":{},"f:proxyNextUpstreamTimeout":{},"f:proxyReadTimeout":{},"f:proxyRequestBuffering":{},"f:proxySendTimeout":{}},"f:pod":{".":{},"f:pullPolicy":{},"f:replicas":{},"f:resources":{".":{},"f:limits":{".":{},"f:cpu":{},"f:memory":{}},"f:requests":{".":{},"f:cpu":{},"f:memory":{}}},"f:strategy":{".":{},"f:rollingUpdate":{".":{},"f:maxSurge":{},"f:maxUnavailable":{}},"f:type":{}},"f:version":{}}}}} {Manager:kubectl-edit Operation:Update APIVersion:app.fylr.io/v1 Time:2021-05-26 23:14:10 +0200 CEST FieldsType:FieldsV1 FieldsV1:{"f:spec":{"f:fylr":{"f:elastic":{"f:parallel":{}},"f:execserver":{"f:parallel":{}}}}}}]} Spec:{Pod:{Replicas:1 Version:develop NodeSelector:map[] Affinity:nil PullPolicy:Always Resources:{Limits:map[cpu:{i:{value:1000 scale:-3} d:{Dec:<nil>} s: Format:DecimalSI} memory:{i:{value:1073741824 scale:0} d:{Dec:<nil>} s: Format:BinarySI}] Requests:map[cpu:{i:{value:64 scale:-3} d:{Dec:<nil>} s:64m Format:DecimalSI} memory:{i:{value:33554432 scale:0} d:{Dec:<nil>} s: Format:BinarySI}]} Strategy:{Type:RollingUpdate RollingUpdate:&RollingUpdateDeployment{MaxUnavailable:1,MaxSurge:1,}}} Ingress:{ClusterIssuer:letsencrypt-staging BodySize:1g ProxyConnectTimeout:300 ProxySendTimeout:300 ProxyReadTimeout:300 ProxyNextUpstreamTimeout:300 ProxyRequestBuffering:off} Fylr:{ExternalURL:example.local AllowPurge:true Authentication:{Application:map[root:admin] Inspect:map[root:admin]} Database:{MaxOpenConns:10 MaxIdleConns:10 DatabaseSecretName:fylr-database-secret} Elastic:{Parallel:5 ObjectsPerJob:100 MaxMem:100mb ElasticSecretName:fylr-elastic-secret} S3:{S3SecretName:fylr-s3-secret} Execserver:{Type:combined Parallel:10 CallbackTimeout:30 Addresses:[http://localhost:8082]}}} Status:{Phase: Replicas: Database: ServiceDNSAddress: ServiceIPv4Address: LastMessages:}}

DELETE

EventType: DELETED
Instance:
&{TypeMeta:{Kind:Instance APIVersion:app.fylr.io/v1} ObjectMeta:{Name:instance-sample GenerateName: Namespace:fylr-sample SelfLink:/apis/app.fylr.io/v1/namespaces/fylr-sample/instances/instance-sample UID:29dc00cb-a9ea-4864-b919-d3de23c15ed7 ResourceVersion:5026123 Generation:1 CreationTimestamp:2021-05-26 23:21:25 +0200 CEST DeletionTimestamp:<nil> DeletionGracePeriodSeconds:<nil> Labels:map[] Annotations:map[kubectl.kubernetes.io/last-applied-configuration:{"apiVersion":"app.fylr.io/v1","kind":"Instance","metadata":{"annotations":{},"name":"instance-sample","namespace":"fylr-sample"},"spec":{"fylr":{"allowPurge":true,"authentication":{"application":{"root":"admin"},"inspect":{"root":"admin"}},"database":{"databaseSecretName":"fylr-database-secret","maxIdleConns":10,"maxOpenConns":10},"elastic":{"elasticSecretName":"fylr-elastic-secret","maxMem":"100mb","objectsPerJob":100,"parallel":2},"execserver":{"addresses":["http://localhost:8082"],"callbackTimeout":30,"parallel":2,"type":"combined"},"externalURL":"example.local","s3":{"s3SecretName":"fylr-s3-secret"}},"ingress":{"bodySize":"1g","clusterIssuer":"letsencrypt-staging","proxyConnectTimeout":300,"proxyNextUpstreamTimeout":300,"proxyReadTimeout":300,"proxyRequestBuffering":"off","proxySendTimeout":300},"pod":{"pullPolicy":"Always","replicas":1,"resources":{"limits":{"cpu":"1000m","memory":"1024Mi"},"requests":{"cpu":"64m","memory":"32Mi"}},"strategy":{"rollingUpdate":{"maxSurge":1,"maxUnavailable":1},"type":"RollingUpdate"},"version":"develop"}}}
] OwnerReferences:[] Finalizers:[] ClusterName: ManagedFields:[{Manager:kubectl-client-side-apply Operation:Update APIVersion:app.fylr.io/v1 Time:2021-05-26 23:21:25 +0200 CEST FieldsType:FieldsV1 FieldsV1:{"f:metadata":{"f:annotations":{".":{},"f:kubectl.kubernetes.io/last-applied-configuration":{}}},"f:spec":{".":{},"f:fylr":{".":{},"f:allowPurge":{},"f:authentication":{".":{},"f:application":{".":{},"f:root":{}},"f:inspect":{".":{},"f:root":{}}},"f:database":{".":{},"f:databaseSecretName":{},"f:maxIdleConns":{},"f:maxOpenConns":{}},"f:elastic":{".":{},"f:elasticSecretName":{},"f:maxMem":{},"f:objectsPerJob":{},"f:parallel":{}},"f:execserver":{".":{},"f:addresses":{},"f:callbackTimeout":{},"f:parallel":{},"f:type":{}},"f:externalURL":{},"f:s3":{".":{},"f:s3SecretName":{}}},"f:ingress":{".":{},"f:bodySize":{},"f:clusterIssuer":{},"f:proxyConnectTimeout":{},"f:proxyNextUpstreamTimeout":{},"f:proxyReadTimeout":{},"f:proxyRequestBuffering":{},"f:proxySendTimeout":{}},"f:pod":{".":{},"f:pullPolicy":{},"f:replicas":{},"f:resources":{".":{},"f:limits":{".":{},"f:cpu":{},"f:memory":{}},"f:requests":{".":{},"f:cpu":{},"f:memory":{}}},"f:strategy":{".":{},"f:rollingUpdate":{".":{},"f:maxSurge":{},"f:maxUnavailable":{}},"f:type":{}},"f:version":{}}}}}]} Spec:{Pod:{Replicas:1 Version:develop NodeSelector:map[] Affinity:nil PullPolicy:Always Resources:{Limits:map[cpu:{i:{value:1000 scale:-3} d:{Dec:<nil>} s: Format:DecimalSI} memory:{i:{value:1073741824 scale:0} d:{Dec:<nil>} s: Format:BinarySI}] Requests:map[cpu:{i:{value:64 scale:-3} d:{Dec:<nil>} s:64m Format:DecimalSI} memory:{i:{value:33554432 scale:0} d:{Dec:<nil>} s: Format:BinarySI}]} Strategy:{Type:RollingUpdate RollingUpdate:&RollingUpdateDeployment{MaxUnavailable:1,MaxSurge:1,}}} Ingress:{ClusterIssuer:letsencrypt-staging BodySize:1g ProxyConnectTimeout:300 ProxySendTimeout:300 ProxyReadTimeout:300 ProxyNextUpstreamTimeout:300 ProxyRequestBuffering:off} Fylr:{ExternalURL:example.local AllowPurge:true Authentication:{Application:map[root:admin] Inspect:map[root:admin]} Database:{MaxOpenConns:10 MaxIdleConns:10 DatabaseSecretName:fylr-database-secret} Elastic:{Parallel:2 ObjectsPerJob:100 MaxMem:100mb ElasticSecretName:fylr-elastic-secret} S3:{S3SecretName:fylr-s3-secret} Execserver:{Type:combined Parallel:2 CallbackTimeout:30 Addresses:[http://localhost:8082]}}} Status:{Phase: Replicas: Database: ServiceDNSAddress: ServiceIPv4Address: LastMessages:}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment