Skip to content

nevermosby/my-crd-controller

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kubernetes CRD & controller for building a website in one step

Goal

Inspired by the implementation of website controller of Kubernetes in Action , You can easily build up a website via a git repo and nginx /w kubernetes workload.

You can find more via the blog:

Development

  1. Define your own crd(CustomResourceDefinitions) struct, for example:

    type Website struct {
        metav1.TypeMeta   `json:",inline"`
        metav1.ObjectMeta `json:"metadata,omitempty"`
    
        Spec   WebsiteSpec   `json:"spec"`
        Status WebsiteStatus `json:"status"`
    }
    
    type WebsiteSpec struct {
        GitRepo        string `json:"gitRepo"`
        DeploymentName string `json:"deploymentName"`
        Replicas       *int32 `json:"replicas"`
    }
    
    type WebsiteStatus struct {
        AvailableReplicas int32 `json:"availableReplicas"`
    }
    
    type WebsiteList struct {
        metav1.TypeMeta `json:",inline"`
        metav1.ListMeta `json:"metadata,omitempty"`
    
        Items []Website `json:"items"`
    }
  2. Generate the controller stuff, not write it

    The kubernetes community provides serveral ways to automatically generate the controller stuff, like:

    I use code generator for this project and wrap it as a shell script codegen.sh:

    ./vendor/k8s.io/code-generator/generate-groups.sh "deepcopy,client,informer,lister" \
    github.com/nevermosby/my-crd-controller/pkg/client \
    github.com/nevermosby/my-crd-controller/pkg/apis "mycontroller:v1alpha1" \
    --go-header-file /Users/davidli/gh/myk8scrd/src/github.com/nevermosby/my-crd-controller/hack/custom-boilerplate.go.txt
  3. Write your event handler

    • Create the nginx deployment based on the git repo
    • Create the nodeport service based on the nginx deployment

Deploy the CRD and controller

  1. Deploy the CRD via YAML
  2. Deploy the controller via deployment

About

my first crd and controller

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published