GithubHelp home page GithubHelp logo

kubernetes-test-controller's Introduction

Kubernetes Test Custom Controller

This is an implementation of a Kubernetes CRD and a custom controller using code-generator. This repository contains one Go module (insujang.github.io/kubernetes-test-controller) with a generated code regarding the CRD (/lib/testresource) and a custom controller based on this (/cmd/controller).

The code generates and uses the following CRD type:

Name:         testresources.insujang.github.io
Namespace:    
Labels:       <none>
Annotations:  <none>
API Version:  apiextensions.k8s.io/v1
Kind:         CustomResourceDefinition
Metadata: <OMITTED>
    Manager:         kube-apiserver
    Operation:       Update
    Time:            2020-04-06T10:07:43Z
  Resource Version:  21349980
  Self Link:         /apis/apiextensions.k8s.io/v1/customresourcedefinitions/testresources.insujang.github.io
  UID:               
Spec:
  Conversion:
    Strategy:  None
  Group:       insujang.github.io
  Names:
    Kind:       TestResource
    List Kind:  TestResourceList
    Plural:     testresources
    Short Names:
      tr
    Singular:               testresource
  Preserve Unknown Fields:  true
  Scope:                    Namespaced
  Versions:
    Additional Printer Columns:
      Json Path:  .spec.command
      Name:       command
      Type:       string
    Name:         v1beta1
    Schema:
      openAPIV3Schema:
        Properties:
          Spec:
            Properties:
              Command:
                Pattern:  ^(echo).*
                Type:     string
              Custom Property:
                Type:  string
            Required:
              command
              customProperty
            Type:  object
        Type:      object
    Served:        true
    Storage:       true
Status:
  Accepted Names:
    Kind:       TestResource
    List Kind:  TestResourceList
    Plural:     testresources
    Short Names:
      tr
    Singular:  testresource
  Conditions:
    Last Transition Time:  2020-04-06T10:07:43Z
    Message:               no conflicts found
    Reason:                NoConflicts
    Status:                True
    Type:                  NamesAccepted
    Last Transition Time:  2020-04-06T10:07:43Z
    Message:               the initial names have been accepted
    Reason:                InitialNamesAccepted
    Status:                True
    Type:                  Established
  Stored Versions:
    v1beta1
Events:  <none>

Directory Structure

1. /lib/testresource

This directory, at first, contains three files for code generation; v1beta1/doc.go, v1beta1/register.go, and v1beta1/types.go. After generating code with code-generator, v1beta1/zz_generated.deepcopy.go and generated are generated.

2. /cmd/controller

This directory contains basic controller logics that creates a CRD type itself, creates a custom resource with the CRD type, and watches events regarding the CRD type.

Usage

Use Dockerfile to build a custom controller. All operations are done by the controller (creating a CRD, creating a custom resource, and watching events).

$ git clone https://github.com/insujang/kubernetes-test-controller
$ cd kubernetes-test-controller
$ docker build -t controller-test .
$ docker run -it --rm --net=host -v $KUBECONFIG:$KUBECONFIG -e KUBECONFIG=$KUBECONFIG controller-test

Expected output

~/kubernetes-test-controller$ docker run -it --rm --net=host -v $KUBECONFIG:$KUBECONFIG -e KUBECONFIG=$KUBECONFIG controller-test
go: downloading k8s.io/api v0.17.0
go: downloading k8s.io/apiextensions-apiserver v0.17.0
go: downloading k8s.io/apimachinery v0.17.0
...
I0407 05:13:44.500578    2240 handler.go:21] Waiting cache to be synced.
I0407 05:13:44.500610    2240 handler.go:33] Starting custom controller.
I0407 05:13:44.500423    2240 handler.go:40] Added: &{{ } {example-tr2  default /apis/insujang.github.io/v1beta1/namespaces/default/testresources/example-tr2 5890795a-81fb-4fe6-85d8-3ec8db89145d 21501514 1 2020-04-07 05:13:44 +0000 UTC <nil> <nil> map[] map[] [] []  [{controller Update insujang.github.io/v1beta1 2020-04-07 05:13:44 +0000 UTC FieldsV1 &FieldsV1{Raw:*[123 34 102 58 115 112 101 99 34 58 123 34 46 34 58 123 125 44 34 102 58 99 111 109 109 97 110 100 34 58 123 125 44 34 102 58 99 117 115 116 111 109 80 114 111 112 101 114 116 121 34 58 123 125 125 44 34 102 58 115 116 97 116 117 115 34 58 123 125 125],}}]} {echo Hello World! asdasd=1234} }
I0407 05:13:44.503252    2240 types.go:72] Updated: &{{ } {example-tr2  default /apis/insujang.github.io/v1beta1/namespaces/default/testresources/example-tr2 5890795a-81fb-4fe6-85d8-3ec8db89145d 21501515 2 2020-04-07 05:13:44 +0000 UTC <nil> <nil> map[] map[] [] []  [{controller Update insujang.github.io/v1beta1 2020-04-07 05:13:44 +0000 UTC FieldsV1 &FieldsV1{Raw:*[123 34 102 58 115 112 101 99 34 58 123 34 46 34 58 123 125 44 34 102 58 99 111 109 109 97 110 100 34 58 123 125 44 34 102 58 99 117 115 116 111 109 80 114 111 112 101 114 116 121 34 58 123 125 125 44 34 102 58 115 116 97 116 117 115 34 58 123 125 125],}}]} {echo Hello World! asdasd=1234} HANDLED}
I0407 05:13:44.503321    2240 event.go:281] Event(v1.ObjectReference{Kind:"TestResource", Namespace:"default", Name:"example-tr2", UID:"5890795a-81fb-4fe6-85d8-3ec8db89145d", APIVersion:"insujang.github.io/v1beta1", ResourceVersion:"21501514", FieldPath:""}): type: 'Normal' reason: 'ObjectHandled' Object is handled by custom controller.

kubernetes-test-controller's People

Contributors

insujang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.