SlideShare a Scribd company logo
https://github.com/operator-framework/operator-sdk
K8s / CNCF Meetup - 2019/02/25
Operator Framework
From Github Project :
The Operator Framework is an open source toolkit to manage Kubernetes
native applications, called Operators, in an effective, automated, and scalable
way.
prune@lecentre.net
Agenda
1. Overview of Operators
2. Operator SDK usage
3. Operators workflow
4. Hands On
5. Conclusion
COYOTE SYSTEM
Who are we ?
A leading provider of community-based driving assistant systems
Founded in 2005 350 employees
1M daily users on a payed
subscription model
2 billion kilometers
traveled every month (1,24
billion miles)
50M members declaration
analyzed every month
Specific know-how in big
data and automotive market
protected by 13 patents
COYOTE SYSTEM
How to be part of the community ?
• Available on a range of Products and Apps,
• … but also with embedded car solutions
COYOTE mini
COYOTE S
COYOTE NAV+
Smartphone APPs
COYOTE SYSTEM
Where does it work ?
… almost everywhere in Europe !
› France
› Belgium
› Netherlands
› Luxembourg
› Italy
› Spain
› Germany
› Poland
› Portugal
Who I am ?
20+ years in Computers / Network / Admin / Devops / Woodworker
Work at Coyote https://www.moncoyote.com/ as System Architect
Github : https://github.com/prune998
Blog (sort of) : https://medium.com/@prune998
Coyote Lab Blog (more to come there) : https://www.mycoyote.ca/blog
Contact : Sebastien “Prune” THOMAS - prune@lecentre.net
What’s an Operator ?
An Operator is an application that deals with the Kubernetes API and Custom
Resources to create/operate new Resources.
It’s an intelligent piece of software that embed the templating to deploy your
resources.
The Operator watch events on the K8s API and react (ex : re-create a pod,
change Labels, update a Secret, Remove a Service…)
What are Custom Resource Definition
CRD are new Resources, like Pods, Deployments, Secrets that you can create.
They are managed through the K8s API the same way as official resources
kubectl get crd
certificates.certmanager.k8s.io 2019-01-25T15:56:53Z
certmerges.certmerge.lecentre.net 2019-01-25T15:57:10Z
prometheuses.monitoring.coreos.com 2019-01-25T16:05:42Z
prometheusrules.monitoring.coreos.com 2019-01-25T16:05:44Z
virtualservices.networking.istio.io 2019-01-25T16:09:16Z
...
Example Custom Resource
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: certmerges.certmerge.lecentre.net
spec:
group: certmerge.lecentre.net
names:
kind: CertMerge
listKind: CertMergeList
plural: certmerges
singular: certmerge
scope: Namespaced
version: v1alpha1
apiVersion: certmerge.lecentre.net/v1alpha1
kind: CertMerge
metadata:
name: "test-certmerge-labels"
spec:
selector:
- labelselector:
matchLabels:
env: "dev"
certmerge: "true"
namespace: default
name: test-cert-labels
namespace: default
Custom Resource Definition (CRD) Custom Resource Manifest (CR)
Before Operators
With Operators
Operators 1
2
3
4
5
6
7
8
9
Operators
Difference with other tools
- Helm / Jsonnet / Ksonnet
They are templating tools. Create a template, set some variables, generate
the Manifests. Once deployed they have no control (tiller does not count).
- StatefulSets / Deployments / Pods
They are K8s Resources. Some minimal feedback to scale/restart, no
dependency between them, no intelligence in management.
- Operators
Watch the K8s API and react in real time. Can have a better control to
scale/restart/configure the target application, with richer features than
Readyness/Liveness Probes
Who needs Operators ?
You may need an Operator if :
- you need to use many times the same Application. ex : deploying one EtcD
cluster in each Namespace
- You need to automate some Resource creation. ex : create some SSL
Certificates inside Secrets (cert-manager), create Prometheus scraping rules
- You need more intelligence in the management. ex : the Etcd-Operator create
and manage Pods directly instead of using a Deployment or StatefulSets
Helm Chart to deploy an Operator ?
- Operators are usually easy to deploy
- use whatever mean you have to deploy them (Helm, Jsonnet, plain manifest
from the Operator creator)
- Once the Operator is running, use the Custom Resources to trigger its power
Existing Operators ?
- Etcd-Operator : https://github.com/coreos/etcd-operator
- Kafka : https://github.com/strimzi/strimzi-kafka-operator
- Nats : https://github.com/nats-io/nats-operator
- Prometheus : https://github.com/coreos/prometheus-operator
- SSL Certificates : https://github.com/jetstack/cert-manager
- RBAC-Manager : https://github.com/reactiveops/rbac-manager
and a lot more, growing...
(check https://github.com/operator-framework/awesome-operators)
Operator all the thing ?
An Operator embed the knowledge and the deployments “templates”.
Don’t create an operator :
- if your application deployment is not stable !
- to deploy one application per cluster (it’s easier to template it)
Create an Operator :
- if you have many users in need to use your resource
- you have a complicated workflow to handle your resource
- you want to (learn to) code in GO (or check other languages operators too)
Operator Creation
Operator SDK (Go) : https://github.com/operator-framework/operator-sdk
- High level APIs and abstractions to write the operational logic more intuitively
- Tools for scaffolding and code generation to bootstrap a new project fast
- Extensions to cover common operator use cases
- Base on official Kubernetes API packages
- Provide common package for leader election for HA Operators
CertMerge Operator - github.com/prune998/certmerge-operator
Install (fast)
mkdir -p $GOPATH/src/github.com/operator-framework
cd $GOPATH/src/github.com/operator-framework
git clone https://github.com/operator-framework/operator-sdk
cd operator-sdk
git checkout v0.4.0
make dep
make install
operator-sdk --version
operator-sdk version v0.4.0+git
Create your operator
mkdir -p $GOPATH/src/github.com/prune998/
cd $GOPATH/src/github.com/prune998/
operator-sdk new certmerge-operator --cluster-scoped
INFO[0000] Create pkg/apis/apis.go
INFO[0000] Create pkg/controller/controller.go
INFO[0000] Create version/version.go
INFO[0000] Create .gitignore
INFO[0000] Create Gopkg.toml
INFO[0000] Run dep ensure ...
INFO[0068] Run dep ensure done
INFO[0068] Run git init ...
INFO[0074] Run git init done
INFO[0074] Project creation complete.
INFO[0000] Creating new Go operator 'certmerge-operator'.
INFO[0000] Create cmd/manager/main.go
INFO[0000] Create build/Dockerfile
INFO[0000] Create build/bin/entrypoint
INFO[0000] Create build/bin/user_setup
INFO[0000] Create deploy/service_account.yaml
INFO[0000] Create deploy/role.yaml
INFO[0000] Create deploy/role_binding.yaml
INFO[0000] Create deploy/operator.yaml
Add API
# Add a new API for the custom resource AppService
operator-sdk add api 
--api-version=certmerge.lecentre.net/v1alpha1 
--kind=CertMerge
This is the basic operation to create the CRD.
It creates files in pkg/apis/certmerge/v1alpha1 including certmerge_types.go which holds the definition of the
CRD :
…
type CertMerge struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec CertMergeSpec `json:"spec,omitempty"`
Status CertMergeStatus `json:"status,omitempty"`
}
...
Add Controler
# Add a new controller that watches for AppService
operator-sdk add controller 
--api-version=certmerge.lecentre.net/v1alpha1 
--kind=CertMerge
Creates files in pkg/controller/certmerge. This is where all your watch and reconcile logic happens
Check doc reference at
https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg#hdr-Controller
type CertMerge struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec CertMergeSpec `json:"spec,omitempty"`
Status CertMergeStatus `json:"status,omitempty"`
}
type CertMergeSpec struct {
SecretName string `json:"name"`
Selector []SecretSelector `json:"selector"`
SecretNamespace string `json:"namespace"`
SecretList []SecretDefinition `json:"secretlist"`
}
type SecretSelector struct {
LabelSelector metav1.LabelSelector `json:"labelselector"`
Namespace string `json:"namespace"`
}
Types (API)
// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r reconcile.Reconciler) error {
// Create a new controller
c, err := controller.New("certmerge-controller", mgr, controller.Options{Reconciler: r})
if err != nil { return err }
// Watch for changes to primary resource CertMerge
err = c.Watch(&source.Kind{Type: &certmergev1alpha1.CertMerge{}}, &handler.EnqueueRequestForObject{})
if err != nil { return err }
// TODO(user): Modify this to be the types you create that are owned by the primary resource
// Watch for changes to secondary resource Pods and requeue the owner CertMerge
err = c.Watch(&source.Kind{Type: &corev1.Secret{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &certmergev1alpha1.CertMerge{},
})
if err != nil { return err }
Watchers
func (r *ReconcileCertMerge) Reconcile(request reconcile.Request) (reconcile.Result, error) {
…
// Fetch the CertMerge instance that triggered this Reconsile
instance := &certmergev1alpha1.CertMerge{}
err := r.client.Get(context.TODO(), request.NamespacedName, instance)
if err != nil {
if errors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue ( by sending `nil` in the error field)
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request. (by sending a non-nil error)
return reconcile.Result{}, err
}
… do some stuff for your operator (see next slide)
}
Reconcile
// Define a new Secret object
secret := newSecretForCR(instance)
// create the DATA for the new secret based on the CertMerge request
certData := make(map[string][]byte)
// Set CertMerge instance as the owner and controller (for garbage collection)
if err := controllerutil.SetControllerReference(instance, secret, r.scheme); err != nil {
return emptyRes, err
}
// build the Cert Data from the secret List provided in the CertMerge Custom Resource
if len(instance.Spec.SecretList) > 0 {
for _, sec := range instance.Spec.SecretList {
secContent, err := r.searchSecretByName(ctx, sec.Name, sec.Namespace)
...
certData[sec.Name+".crt"] = secContent.Data["tls.crt"]
certData[sec.Name+".key"] = secContent.Data["tls.key"]
}
}
// add the Data to the secret
secret.Data = certData
// create the new secret
if err := r.client.Create(ctx, secret); err != nil {...}
Reconcile 2
Generate and build
# re-generate all the files that depend on the CRD API
operator-sdk generate k8s
# re-generate the CRD Manifest (rarely used, when you change your API name)
operator-sdk generate openapi
# build the operator (aka go build)
operator-sdk build prune/certmerge-operator:v0.0.1
Operator workflow (easy)
Operator workflow (harder with Predicate)
Hands-on
switch to the console / code
Conclusion
● Operator SDK make it really easy
● using K8s primitives (and go-client), not “vendor” dependent
● Operators can be declined in Controlers (admission)
● You need to learn a little bit of the K8s API to get to cool stuff
References
● https://github.com/operator-framework/operator-sdk
● https://github.com/operator-framework/operator-sdk/blob/master/doc/user-guide.md
● https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg#hdr-Controller
● https://github.com/operator-framework/operator-sdk/blob/master/doc/user/event-filtering.md

More Related Content

PDF
Kubernetes Networking | Kubernetes Services, Pods & Ingress Networks | Kubern...
PDF
Deploy Application on Kubernetes
PDF
Kubernetes extensibility: CRDs & Operators
PDF
An intro to Kubernetes operators
PDF
Kubernetes Interview Questions And Answers | Kubernetes Tutorial | Kubernetes...
PDF
Kubernetes Application Deployment with Helm - A beginner Guide!
PDF
Kubernetes a comprehensive overview
PDF
Helm - Package Manager for Kubernetes
Kubernetes Networking | Kubernetes Services, Pods & Ingress Networks | Kubern...
Deploy Application on Kubernetes
Kubernetes extensibility: CRDs & Operators
An intro to Kubernetes operators
Kubernetes Interview Questions And Answers | Kubernetes Tutorial | Kubernetes...
Kubernetes Application Deployment with Helm - A beginner Guide!
Kubernetes a comprehensive overview
Helm - Package Manager for Kubernetes

What's hot (20)

PDF
Extending kubernetes with CustomResourceDefinitions
PDF
Hands-On Introduction to Kubernetes at LISA17
PPTX
Kubernetes Introduction
PDF
GitHub Actions in action
PDF
Kubernetes security
PPTX
Introduction to kubernetes
PPTX
Spring Cloud Config
PPT
Docker introduction
PDF
Kubernetes Architecture | Understanding Kubernetes Components | Kubernetes Tu...
PDF
Gitlab, GitOps & ArgoCD
PPTX
Introduction to Docker - 2017
PDF
Free GitOps Workshop + Intro to Kubernetes & GitOps
PDF
Kubernetes - A Comprehensive Overview
PDF
Getting Started with Kubernetes
PDF
Kubernetes architecture
PDF
Kubernetes: A Short Introduction (2019)
PPTX
Kubernetes 101 for Beginners
PDF
(Draft) Kubernetes - A Comprehensive Overview
PDF
GitOps with ArgoCD
PDF
Introduction to Kubernetes Workshop
Extending kubernetes with CustomResourceDefinitions
Hands-On Introduction to Kubernetes at LISA17
Kubernetes Introduction
GitHub Actions in action
Kubernetes security
Introduction to kubernetes
Spring Cloud Config
Docker introduction
Kubernetes Architecture | Understanding Kubernetes Components | Kubernetes Tu...
Gitlab, GitOps & ArgoCD
Introduction to Docker - 2017
Free GitOps Workshop + Intro to Kubernetes & GitOps
Kubernetes - A Comprehensive Overview
Getting Started with Kubernetes
Kubernetes architecture
Kubernetes: A Short Introduction (2019)
Kubernetes 101 for Beginners
(Draft) Kubernetes - A Comprehensive Overview
GitOps with ArgoCD
Introduction to Kubernetes Workshop
Ad

Similar to Operator SDK for K8s using Go (20)

PDF
Extending Kubernetes with Operators
PDF
Kubernetes extensibility: crd & operators
PPTX
Kubernetes Info Operators Operators Operators
PDF
Intro to creating kubernetes operators
PDF
Kubernetes Operators Automating the Container Orchestration Platform 1st Edit...
PPTX
How kubernetes operators can rescue dev secops in midst of a pandemic updated
PDF
A Series of Fortunate Events: Building an Operator in Java
PDF
Red Hat OpenShift Operators - Operators ABC
PDF
Kubernetes Operators 1st Edition Jason Dobies
PDF
Kubernetes Operators Automating the Container Orchestration Platform 1st Edit...
PDF
Kubernetes Operators 1st Edition Jason Dobies
PDF
DoK Talks #91- Leveraging Druid Operator to manage Apache Druid on Kubernetes
PPTX
Demystifying k8s operators
PDF
Operator Lifecycle Management
PDF
Operator Lifecycle Management
PDF
The Operator Pattern - Managing Stateful Services in Kubernetes
PDF
DevOpSec_KubernetesOperatorUsingJava.pdf
PPTX
Ofir Makmal - Intro To Kubernetes Operators - Google Cloud Summit 2018 Tel Aviv
PDF
Deploying Anything as a Service (XaaS) Using Operators on Kubernetes
PDF
JS Fest 2019. Peter Braun. DevOps in the Cluster: A deep dive into operators
Extending Kubernetes with Operators
Kubernetes extensibility: crd & operators
Kubernetes Info Operators Operators Operators
Intro to creating kubernetes operators
Kubernetes Operators Automating the Container Orchestration Platform 1st Edit...
How kubernetes operators can rescue dev secops in midst of a pandemic updated
A Series of Fortunate Events: Building an Operator in Java
Red Hat OpenShift Operators - Operators ABC
Kubernetes Operators 1st Edition Jason Dobies
Kubernetes Operators Automating the Container Orchestration Platform 1st Edit...
Kubernetes Operators 1st Edition Jason Dobies
DoK Talks #91- Leveraging Druid Operator to manage Apache Druid on Kubernetes
Demystifying k8s operators
Operator Lifecycle Management
Operator Lifecycle Management
The Operator Pattern - Managing Stateful Services in Kubernetes
DevOpSec_KubernetesOperatorUsingJava.pdf
Ofir Makmal - Intro To Kubernetes Operators - Google Cloud Summit 2018 Tel Aviv
Deploying Anything as a Service (XaaS) Using Operators on Kubernetes
JS Fest 2019. Peter Braun. DevOps in the Cluster: A deep dive into operators
Ad

More from CloudOps2005 (20)

PDF
Defense in Depth: Securing your new Kubernetes cluster from the challenges th...
PPTX
Human No, Machine Yes: Welcome to the CDF with Incremental Confidence
PDF
The Salmon Algorithm Spawning with Kubernetes
PDF
Own your Destiny in the Cloud - Ian Rae - Cloud Native Day Montreal 2019
PDF
Plateformes et infrastructure infonuagique natif de ville de Montréall
PPTX
Using Rook to Manage Kubernetes Storage with Ceph
PDF
Kafka on Kubernetes
PDF
Kubernetes: Crossing the Chasm
PPTX
Distributed Logging with Kubernetes
PDF
Kubernetes Security with Calico and Open Policy Agent
PDF
Advanced Deployment Strategies with Kubernetes and Istio
PPTX
Kubernetes Services are sooo Yesterday!
PPTX
Amazon EKS: the good, the bad, and the ugly
PDF
Kubernetes, Terraform, Vault, and Consul
PDF
SIG Multicluster and the Path to Federation
PDF
To Russia with Love: Deploying Kubernetes in Exotic Locations On Prem
PPTX
How to Handle your Kubernetes Upgrades
PDF
Kubernetes and Cloud Native Meetup - March, 2019
PDF
Prometheus and Thanos
PPTX
Jenkins as Code
Defense in Depth: Securing your new Kubernetes cluster from the challenges th...
Human No, Machine Yes: Welcome to the CDF with Incremental Confidence
The Salmon Algorithm Spawning with Kubernetes
Own your Destiny in the Cloud - Ian Rae - Cloud Native Day Montreal 2019
Plateformes et infrastructure infonuagique natif de ville de Montréall
Using Rook to Manage Kubernetes Storage with Ceph
Kafka on Kubernetes
Kubernetes: Crossing the Chasm
Distributed Logging with Kubernetes
Kubernetes Security with Calico and Open Policy Agent
Advanced Deployment Strategies with Kubernetes and Istio
Kubernetes Services are sooo Yesterday!
Amazon EKS: the good, the bad, and the ugly
Kubernetes, Terraform, Vault, and Consul
SIG Multicluster and the Path to Federation
To Russia with Love: Deploying Kubernetes in Exotic Locations On Prem
How to Handle your Kubernetes Upgrades
Kubernetes and Cloud Native Meetup - March, 2019
Prometheus and Thanos
Jenkins as Code

Recently uploaded (20)

PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Modernizing your data center with Dell and AMD
PDF
KodekX | Application Modernization Development
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Approach and Philosophy of On baking technology
PDF
cuic standard and advanced reporting.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Electronic commerce courselecture one. Pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Modernizing your data center with Dell and AMD
KodekX | Application Modernization Development
Spectral efficient network and resource selection model in 5G networks
Network Security Unit 5.pdf for BCA BBA.
Agricultural_Statistics_at_a_Glance_2022_0.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Approach and Philosophy of On baking technology
cuic standard and advanced reporting.pdf
Encapsulation_ Review paper, used for researhc scholars
Electronic commerce courselecture one. Pdf
Per capita expenditure prediction using model stacking based on satellite ima...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Encapsulation theory and applications.pdf
Review of recent advances in non-invasive hemoglobin estimation
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

Operator SDK for K8s using Go

  • 1. https://github.com/operator-framework/operator-sdk K8s / CNCF Meetup - 2019/02/25 Operator Framework From Github Project : The Operator Framework is an open source toolkit to manage Kubernetes native applications, called Operators, in an effective, automated, and scalable way. prune@lecentre.net
  • 2. Agenda 1. Overview of Operators 2. Operator SDK usage 3. Operators workflow 4. Hands On 5. Conclusion
  • 3. COYOTE SYSTEM Who are we ? A leading provider of community-based driving assistant systems Founded in 2005 350 employees 1M daily users on a payed subscription model 2 billion kilometers traveled every month (1,24 billion miles) 50M members declaration analyzed every month Specific know-how in big data and automotive market protected by 13 patents
  • 4. COYOTE SYSTEM How to be part of the community ? • Available on a range of Products and Apps, • … but also with embedded car solutions COYOTE mini COYOTE S COYOTE NAV+ Smartphone APPs
  • 5. COYOTE SYSTEM Where does it work ? … almost everywhere in Europe ! › France › Belgium › Netherlands › Luxembourg › Italy › Spain › Germany › Poland › Portugal
  • 6. Who I am ? 20+ years in Computers / Network / Admin / Devops / Woodworker Work at Coyote https://www.moncoyote.com/ as System Architect Github : https://github.com/prune998 Blog (sort of) : https://medium.com/@prune998 Coyote Lab Blog (more to come there) : https://www.mycoyote.ca/blog Contact : Sebastien “Prune” THOMAS - prune@lecentre.net
  • 7. What’s an Operator ? An Operator is an application that deals with the Kubernetes API and Custom Resources to create/operate new Resources. It’s an intelligent piece of software that embed the templating to deploy your resources. The Operator watch events on the K8s API and react (ex : re-create a pod, change Labels, update a Secret, Remove a Service…)
  • 8. What are Custom Resource Definition CRD are new Resources, like Pods, Deployments, Secrets that you can create. They are managed through the K8s API the same way as official resources kubectl get crd certificates.certmanager.k8s.io 2019-01-25T15:56:53Z certmerges.certmerge.lecentre.net 2019-01-25T15:57:10Z prometheuses.monitoring.coreos.com 2019-01-25T16:05:42Z prometheusrules.monitoring.coreos.com 2019-01-25T16:05:44Z virtualservices.networking.istio.io 2019-01-25T16:09:16Z ...
  • 9. Example Custom Resource apiVersion: apiextensions.k8s.io/v1beta1 kind: CustomResourceDefinition metadata: name: certmerges.certmerge.lecentre.net spec: group: certmerge.lecentre.net names: kind: CertMerge listKind: CertMergeList plural: certmerges singular: certmerge scope: Namespaced version: v1alpha1 apiVersion: certmerge.lecentre.net/v1alpha1 kind: CertMerge metadata: name: "test-certmerge-labels" spec: selector: - labelselector: matchLabels: env: "dev" certmerge: "true" namespace: default name: test-cert-labels namespace: default Custom Resource Definition (CRD) Custom Resource Manifest (CR)
  • 14. Difference with other tools - Helm / Jsonnet / Ksonnet They are templating tools. Create a template, set some variables, generate the Manifests. Once deployed they have no control (tiller does not count). - StatefulSets / Deployments / Pods They are K8s Resources. Some minimal feedback to scale/restart, no dependency between them, no intelligence in management. - Operators Watch the K8s API and react in real time. Can have a better control to scale/restart/configure the target application, with richer features than Readyness/Liveness Probes
  • 15. Who needs Operators ? You may need an Operator if : - you need to use many times the same Application. ex : deploying one EtcD cluster in each Namespace - You need to automate some Resource creation. ex : create some SSL Certificates inside Secrets (cert-manager), create Prometheus scraping rules - You need more intelligence in the management. ex : the Etcd-Operator create and manage Pods directly instead of using a Deployment or StatefulSets
  • 16. Helm Chart to deploy an Operator ? - Operators are usually easy to deploy - use whatever mean you have to deploy them (Helm, Jsonnet, plain manifest from the Operator creator) - Once the Operator is running, use the Custom Resources to trigger its power
  • 17. Existing Operators ? - Etcd-Operator : https://github.com/coreos/etcd-operator - Kafka : https://github.com/strimzi/strimzi-kafka-operator - Nats : https://github.com/nats-io/nats-operator - Prometheus : https://github.com/coreos/prometheus-operator - SSL Certificates : https://github.com/jetstack/cert-manager - RBAC-Manager : https://github.com/reactiveops/rbac-manager and a lot more, growing... (check https://github.com/operator-framework/awesome-operators)
  • 18. Operator all the thing ? An Operator embed the knowledge and the deployments “templates”. Don’t create an operator : - if your application deployment is not stable ! - to deploy one application per cluster (it’s easier to template it) Create an Operator : - if you have many users in need to use your resource - you have a complicated workflow to handle your resource - you want to (learn to) code in GO (or check other languages operators too)
  • 19. Operator Creation Operator SDK (Go) : https://github.com/operator-framework/operator-sdk - High level APIs and abstractions to write the operational logic more intuitively - Tools for scaffolding and code generation to bootstrap a new project fast - Extensions to cover common operator use cases - Base on official Kubernetes API packages - Provide common package for leader election for HA Operators
  • 20. CertMerge Operator - github.com/prune998/certmerge-operator
  • 21. Install (fast) mkdir -p $GOPATH/src/github.com/operator-framework cd $GOPATH/src/github.com/operator-framework git clone https://github.com/operator-framework/operator-sdk cd operator-sdk git checkout v0.4.0 make dep make install operator-sdk --version operator-sdk version v0.4.0+git
  • 22. Create your operator mkdir -p $GOPATH/src/github.com/prune998/ cd $GOPATH/src/github.com/prune998/ operator-sdk new certmerge-operator --cluster-scoped INFO[0000] Create pkg/apis/apis.go INFO[0000] Create pkg/controller/controller.go INFO[0000] Create version/version.go INFO[0000] Create .gitignore INFO[0000] Create Gopkg.toml INFO[0000] Run dep ensure ... INFO[0068] Run dep ensure done INFO[0068] Run git init ... INFO[0074] Run git init done INFO[0074] Project creation complete. INFO[0000] Creating new Go operator 'certmerge-operator'. INFO[0000] Create cmd/manager/main.go INFO[0000] Create build/Dockerfile INFO[0000] Create build/bin/entrypoint INFO[0000] Create build/bin/user_setup INFO[0000] Create deploy/service_account.yaml INFO[0000] Create deploy/role.yaml INFO[0000] Create deploy/role_binding.yaml INFO[0000] Create deploy/operator.yaml
  • 23. Add API # Add a new API for the custom resource AppService operator-sdk add api --api-version=certmerge.lecentre.net/v1alpha1 --kind=CertMerge This is the basic operation to create the CRD. It creates files in pkg/apis/certmerge/v1alpha1 including certmerge_types.go which holds the definition of the CRD : … type CertMerge struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` Spec CertMergeSpec `json:"spec,omitempty"` Status CertMergeStatus `json:"status,omitempty"` } ...
  • 24. Add Controler # Add a new controller that watches for AppService operator-sdk add controller --api-version=certmerge.lecentre.net/v1alpha1 --kind=CertMerge Creates files in pkg/controller/certmerge. This is where all your watch and reconcile logic happens Check doc reference at https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg#hdr-Controller
  • 25. type CertMerge struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` Spec CertMergeSpec `json:"spec,omitempty"` Status CertMergeStatus `json:"status,omitempty"` } type CertMergeSpec struct { SecretName string `json:"name"` Selector []SecretSelector `json:"selector"` SecretNamespace string `json:"namespace"` SecretList []SecretDefinition `json:"secretlist"` } type SecretSelector struct { LabelSelector metav1.LabelSelector `json:"labelselector"` Namespace string `json:"namespace"` } Types (API)
  • 26. // add adds a new Controller to mgr with r as the reconcile.Reconciler func add(mgr manager.Manager, r reconcile.Reconciler) error { // Create a new controller c, err := controller.New("certmerge-controller", mgr, controller.Options{Reconciler: r}) if err != nil { return err } // Watch for changes to primary resource CertMerge err = c.Watch(&source.Kind{Type: &certmergev1alpha1.CertMerge{}}, &handler.EnqueueRequestForObject{}) if err != nil { return err } // TODO(user): Modify this to be the types you create that are owned by the primary resource // Watch for changes to secondary resource Pods and requeue the owner CertMerge err = c.Watch(&source.Kind{Type: &corev1.Secret{}}, &handler.EnqueueRequestForOwner{ IsController: true, OwnerType: &certmergev1alpha1.CertMerge{}, }) if err != nil { return err } Watchers
  • 27. func (r *ReconcileCertMerge) Reconcile(request reconcile.Request) (reconcile.Result, error) { … // Fetch the CertMerge instance that triggered this Reconsile instance := &certmergev1alpha1.CertMerge{} err := r.client.Get(context.TODO(), request.NamespacedName, instance) if err != nil { if errors.IsNotFound(err) { // Request object not found, could have been deleted after reconcile request. // Owned objects are automatically garbage collected. For additional cleanup logic use finalizers. // Return and don't requeue ( by sending `nil` in the error field) return reconcile.Result{}, nil } // Error reading the object - requeue the request. (by sending a non-nil error) return reconcile.Result{}, err } … do some stuff for your operator (see next slide) } Reconcile
  • 28. // Define a new Secret object secret := newSecretForCR(instance) // create the DATA for the new secret based on the CertMerge request certData := make(map[string][]byte) // Set CertMerge instance as the owner and controller (for garbage collection) if err := controllerutil.SetControllerReference(instance, secret, r.scheme); err != nil { return emptyRes, err } // build the Cert Data from the secret List provided in the CertMerge Custom Resource if len(instance.Spec.SecretList) > 0 { for _, sec := range instance.Spec.SecretList { secContent, err := r.searchSecretByName(ctx, sec.Name, sec.Namespace) ... certData[sec.Name+".crt"] = secContent.Data["tls.crt"] certData[sec.Name+".key"] = secContent.Data["tls.key"] } } // add the Data to the secret secret.Data = certData // create the new secret if err := r.client.Create(ctx, secret); err != nil {...} Reconcile 2
  • 29. Generate and build # re-generate all the files that depend on the CRD API operator-sdk generate k8s # re-generate the CRD Manifest (rarely used, when you change your API name) operator-sdk generate openapi # build the operator (aka go build) operator-sdk build prune/certmerge-operator:v0.0.1
  • 31. Operator workflow (harder with Predicate)
  • 32. Hands-on switch to the console / code
  • 33. Conclusion ● Operator SDK make it really easy ● using K8s primitives (and go-client), not “vendor” dependent ● Operators can be declined in Controlers (admission) ● You need to learn a little bit of the K8s API to get to cool stuff
  • 34. References ● https://github.com/operator-framework/operator-sdk ● https://github.com/operator-framework/operator-sdk/blob/master/doc/user-guide.md ● https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg#hdr-Controller ● https://github.com/operator-framework/operator-sdk/blob/master/doc/user/event-filtering.md