Upgrade to Pro — share decks privately, control downloads, hide ads and more …

k8s_cncf_meetup_08_02_2023.pdf

 k8s_cncf_meetup_08_02_2023.pdf

Vincent Van der Kussen

February 09, 2023
Tweet

More Decks by Vincent Van der Kussen

Other Decks in Technology

Transcript

  1. cdk8s, a YAML alternative cure Vincent Van der Kussen Thursday,

    February 8, 2023 Belgium Kubernetes & CNCF Meetup
  2. #whoami ▪ Retired Linux engineer ▪ Freelance “cloud native” engineer

    ▪ @vincentvdk (Twitter/Mastodon/Github) ▪ Co-owner of Autops
  3. Plain YAML Ok for small things Gets harder as you

    grow • copy/paste • Manual updates are complex
  4. Source: https://kubernetespodcast.com/episode/012-kubernetes-origins/ Also intersting Kubernetes: The Documentary (YouTube) “And so

    there was always this thinking that there would be higher-level abstractions that would help to sort of take the intent and translate that into those API objects. I'm kind of horrified that we're still authoring those API objects directly by hand because we never intended those things to be human consumable.” - Joe Beda
  5. • Still manipulate YAML in some way • Testing? •

    No IDE/Editor integration − Linting, code completion, navigation, .. • Re-usability • Simplicity
  6. Share as libraries Share reusable components through code libraries Code

    libraries are easier to share and maintain than templates. CDK8s allows you to standardize how to do things in Kubernetes and share this across your organization
  7. Construct levels • L0 – ApiObject − 1-to-1 mapping with

    Kubernetes objects − You will probably never use these • L1 – Higher level (ex: Deployment) − cdk8s+ • custom – ex: MyCompanyService − Collection of L1
  8. 1 import { Construct } from 'constructs'; 2 import {

    App, Chart, ChartProps } from 'cdk8s'; 3 import {CompanyService} from './lib/company-service' 4 5 export class MyChart extends Chart { 6 constructor(scope: Construct, id: string, props: ChartProps = { }) { 7 super(scope, id, props); 8 9 10 new CompanyService(this, 'NewService', { 11 name: 'foo', 12 image: 'nginx' 13 }) 14 15 } 16 } 17 18 const app = new App(); 19 new MyChart(app, 'example2'); 20 app.synth(); 21
  9. 1 import {MyApp} from './main'; 2 import {Testing} from 'cdk8s';

    3 4 describe('Placeholder', () => { 5 test('Label shoud match', () => { 6 const app = Testing.app(); 7 const chart = new MyApp(app, 'test-chart', {env: 'production'}); 8 const results = Testing.synth(chart) 9 expect(results[0]).toHaveProperty('metadata.labels.env', 'production') 10 }) 11 12 test('Production needs 2 replicas', () => { 13 const app = Testing.app(); 14 const chart = new MyApp(app, 'test-chart', {env: 'production'}); 15 const results = Testing.synth(chart) 16 //expect(results[0]).toHaveProperty('spec.replicas', 2) 17 const deploy = results.filter(v => { return v.kind == 'Deployment'}) 18 expect(deploy[0]).toHaveProperty('spec.replicas', 2); 19 }) 20 }); 21
  10. Helm support 1 class MyChart extends cdk8s.Chart { 2 constructor(scope:

    Construct, id: string) { 3 super(scope, id); 4 5 const redis = new Helm(this, 'redis', { 6 chart: 'bitnami/redis', 7 values: { 8 sentinel: { 9 enabled: true 10 } 11 } 12 }); 13 } 14 } 1 const master = redis.apiObjects.find(o => o.name === 'foo-redis-master'); 2 master.metadata.addAnnotation('my.annotation', 'hey-there');
  11. 1 export class Redis extends cdk8s.Chart { 2 3 public

    readonly password: SecretValue; 4 5 constructor(scope: Construct, id: string) { 6 super(scope, id); 7 8 const redis = new Helm(this, 'redis', { 9 chart: 'bitnami/redis', 10 values: { 11 sentinel: { 12 enabled: true 13 } 14 } 15 }); 16 } 17 18 // Exporting the secret created by Helm chart 19 this.password = { 20 secret: secret.fromSecretName(redis.releaseName), 21 key: 'redis-password' 22 }; 23 } 1 const redis = new Redis(this, 'redis') 2 // 3 env: { 4 REDIS_PASWORD: EnvValue.fromSecretvalue(redis.password) 5 }
  12. Escape Hatches 1 import { JsonPatch } from 'cdk8s'; 2

    apiObject.addJsonPatch(JsonPatch.replace('/foo', 'bar')); 3 apiObject.addJsonPatch(JsonPatch.add('/foo/bar/0', { bar: 123 }));