Skip to content

Instantly share code, notes, and snippets.

View trstringer's full-sized avatar

Thomas Stringer trstringer

View GitHub Profile
package main
import (
"fmt"
"sync"
"time"
)
func main() {
fmt.Println("Starting program...")
$ kubectl create ns imgpullpolicy
$ kubectl apply -f pod_k8s_1_9.yaml -n imgpullpolicy
$ kubectl get po -n imgpullpolicy test-image-pull-policy -o jsonpath="{.spec.containers[0].imagePullPolicy}"
Always
apiVersion: v1
kind: Pod
metadata:
name: test-image-pull-policy
spec:
containers:
- name: nginx
image: nginx:1.13
imagePullPolicy: IfNotPresent
// get the Kubernetes client for connectivity
client := getKubernetesClient()
// create the informer so that we can not only list resources
// but also watch them for all pods in the default namespace
informer := cache.NewSharedIndexInformer(
// the ListWatch contains two different functions that our
// informer requires: ListFunc to take care of listing and watching
// the resources we want to handle
&cache.ListWatch{
apiVersion: trstringer.com/v1
kind: MyResource
metadata:
name: example-myresource
spec:
message: hello world
someValue: 13
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: myresources.trstringer.com
spec:
group: trstringer.com
version: v1
names:
kind: MyResource
plural: myresources
func main() {
// get the Kubernetes client for connectivity
client, myresourceClient := getKubernetesClient()
// retrieve our custom resource informer which was generated from
// the code generator and pass it the custom resource client, specifying
// we should be looking through all namespaces for listing and watching
informer := myresourceinformer_v1.NewMyResourceInformer(
myresourceClient,
meta_v1.NamespaceAll,
// retrieve the Kubernetes cluster client from outside of the cluster
func getKubernetesClient() (kubernetes.Interface, myresourceclientset.Interface) {
// construct the path to resolve to `~/.kube/config`
kubeConfigPath := os.Getenv("HOME") + "/.kube/config"
// create the config from the path
config, err := clientcmd.BuildConfigFromFlags("", kubeConfigPath)
if err != nil {
log.Fatalf("getClusterConfig: %v", err)
}
package v1
import (
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"github.com/trstringer/k8s-controller-core-resource/pkg/apis/myresource"
)
// +k8s:deepcopy-gen=package
// +groupName=trstringer.com
package v1