PHP Classes

How to Generate Kubernetes YAML Configuration Files with Kubernetes Resource generator: Generate a configuration for a Kubernetes resource

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 28 All time: 11,158 This week: 455Up
Version License PHP version Categories
kubernetes-resource- 1.0MIT/X Consortium ...8.1PHP 5, Tools, Systems administration, C...
Description 

Author

This package can generate a configuration for a Kubernetes resource.

It provides classes to define the property values of several types of resources used in Kubernetes, like labels, containers, and pods.

The package can generate a configuration file in YAML format for a pod previously defined to be associated with a container with given labels.

Innovation Award
PHP Programming Innovation award nominee
October 2022
Number 3
Kubernetes is a system to manage containers used in many cloud hosting systems.

Containers are helpful for managing and running applications that can run in an isolated way, including those that implement PHP-based Web applications.

This package provides a simplified way to generate configuration files to use with Kubernetes containers from given parameters.

Manuel Lemos
Picture of Angel Campos
Name: Angel Campos <contact>
Classes: 8 packages by
Country: Spain Spain
Age: ???
All time rank: 377599 in Spain Spain
Week rank: 192 Up6 in Spain Spain Up
Innovation award
Innovation award
Nominee: 4x

Documentation

kubernetes-resource-generator

Maintainability Rating Reliability Rating Security Rating Vulnerabilities Coverage

a PHP package to generate yaml files of Kubernetes resources.

Generate a Pod definition

use Acamposm\K8sResourceGenerator\Reference\Annotations\PodAnnotations;
use Acamposm\K8sResourceGenerator\Reference\Labels\PodLabels;
use Acamposm\K8sResourceGenerator\Enums\ImagePullPolicy;
use Acamposm\K8sResourceGenerator\Enums\OperatingSystem;
use Acamposm\K8sResourceGenerator\Enums\RestartPolicy;
use Acamposm\K8sResourceGenerator\Resources\Container;
use Acamposm\K8sResourceGenerator\Resources\Pod;

// Set well known kubernetes annotations
$podAnnotations = array_merge(
    PodAnnotations::deletionCost(500),
    PodAnnotations::defaultContainer('my-app-container'),
);

// Set common used kubernetes labels
$podLabels = array_merge(
    PodLabels::component('database'),
    PodLabels::instance('my-awesome-application-xyz'),
    PodLabels::managedBy('Kustomize'),
    PodLabels::name('my-awesome-application'),
    PodLabels::partOf('my-awesome-application'),
    PodLabels::version('1.0.0'),
);

// Now set a container for the pod
$container = new Container();
$container->name('app-name')
    ->addEnvVariable('DEBUG', '*')
    ->addPorts([
        [
            'containerPort' => 4000,
            'name' => 'http-alt',
            'protocol' => 'TCP'
        ]
    ])
    ->image('alpine:latest')
    ->imagePullPolicy(ImagePullPolicy::ALWAYS)
    ->setCpuRequest('100m')
    ->setCpuLimit(1)
    ->setMemoryLimit('120Mi')
    ->setMemoryRequest('1Gi');

// Finally fill the Pod values 
$pod = new Pod();
$pod->name('pod-name')
    ->namespace('my-awesome-project')
    ->addAnnotation('imageregistry', 'https://hub.docker.com/')
    ->addLabels($podLabels)
    ->addContainers([$container->toArray()])
    ->addImagePullSecret('registry-access-secret')
    ->addNodeSelectors([
        'type' => 'compute',
        'diskType' => 'ssd'
    ])
    ->osName(OperatingSystem::LINUX)
    ->restartPolicy(RestartPolicy::NEVER)
    ->serviceAccount('pod-service-account')
    ->toYaml();

Use toYaml() method to generate a YAML representation of the Kubernetes resource.

apiVersion: v1
kind: Pod
metadata:
  name: pod-name
  namespace: my-awesome-project
  annotations:
    controller.kubernetes.io/pod-deletion-cost: 500
    imageregistry: 'https://hub.docker.com/'
    kubectl.kubernetes.io/default-container: my-app-container
  labels:
    app.kubernetes.io/component: database
    app.kubernetes.io/instance: my-awesome-application-xyz
    app.kubernetes.io/managed-by: Kustomize
    app.kubernetes.io/name: my-awesome-application
    app.kubernetes.io/part-of: my-awesome-application
    app.kubernetes.io/version: 1.0.0
spec:
  os:
    name: linux
  containers:
    - env:
        - name: DEBUG
          value: '*'
      image: 'alpine:latest'
      imagePullPolicy: Always
      name: app-name
      ports:
        - containerPort: 4000
          name: http-alt
          protocol: TCP
      resources:
        limits:
          cpu: '1'
          memory: '120Mi'
        requests:
          cpu: '100m'
          memory: '1Gi'
      imagePullSecrets:
        - name: registry-access-secret
      nodeSelector:
        type: compute
        diskType: ssd
      restartPolicy: Never
      serviceAccount: pod-service-account

  Files folder image Files (50)  
File Role Description
Files folder image.github (1 directory)
Files folder imagedocs (1 file)
Files folder imagesrc (5 files, 7 directories)
Files folder imagetests (3 directories)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file CODE_OF_CONDUCT.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file sonar-project.properties Data Auxiliary data

  Files folder image Files (50)  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files (50)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file CI.yaml Data Auxiliary data

  Files folder image Files (50)  /  docs  
File Role Description
  Accessible without login Plain text file well-known-labels-and-annotations.md Data Auxiliary data

  Files folder image Files (50)  /  src  
File Role Description
Files folder imageContracts (1 file)
Files folder imageEnums (3 files)
Files folder imageExceptions (1 file)
Files folder imageHelpers (3 files)
Files folder imageReference (2 directories)
Files folder imageResources (10 files)
Files folder imageTraits (2 files)
  Plain text file K8sConfigurationResource.php Class Class source
  Plain text file K8sNetworkResource.php Class Class source
  Plain text file K8sResource.php Class Class source
  Plain text file K8sStorageResource.php Class Class source
  Plain text file K8sWorkloadResource.php Class Class source

  Files folder image Files (50)  /  src  /  Contracts  
File Role Description
  Plain text file KubernetesResource.php Class Class source

  Files folder image Files (50)  /  src  /  Enums  
File Role Description
  Accessible without login Plain text file ImagePullPolicy.php Aux. Auxiliary script
  Accessible without login Plain text file OperatingSystem.php Aux. Auxiliary script
  Accessible without login Plain text file RestartPolicy.php Aux. Auxiliary script

  Files folder image Files (50)  /  src  /  Exceptions  
File Role Description
  Plain text file UnexpectedUnitSuffixException.php Class Class source

  Files folder image Files (50)  /  src  /  Helpers  
File Role Description
  Plain text file KubernetesRecommendedLabels.php Class Class source
  Plain text file ResourceUnit.php Class Class source
  Plain text file Yaml.php Class Class source

  Files folder image Files (50)  /  src  /  Reference  
File Role Description
Files folder imageAnnotations (3 files)
Files folder imageLabels (3 files)

  Files folder image Files (50)  /  src  /  Reference  /  Annotations  
File Role Description
  Plain text file AllObjectAnnotations.php Class Class source
  Plain text file PodAnnotations.php Class Class source
  Plain text file ServiceAccountAnnotations.php Class Class source

  Files folder image Files (50)  /  src  /  Reference  /  Labels  
File Role Description
  Plain text file AllObjectLabels.php Class Class source
  Plain text file NodeLabels.php Class Class source
  Plain text file PodLabels.php Class Class source

  Files folder image Files (50)  /  src  /  Resources  
File Role Description
  Plain text file ConfigMap.php Class Class source
  Plain text file Container.php Class Class source
  Plain text file DaemonSet.php Class Class source
  Plain text file Deployment.php Class Class source
  Plain text file Ingress.php Class Class source
  Plain text file Pod.php Class Class source
  Plain text file ReplicaSet.php Class Class source
  Plain text file Secret.php Class Class source
  Plain text file Service.php Class Class source
  Plain text file StatefulSet.php Class Class source

  Files folder image Files (50)  /  src  /  Traits  
File Role Description
  Plain text file CanCheckProperties.php Class Class source
  Plain text file Exportable.php Class Class source

  Files folder image Files (50)  /  tests  
File Role Description
Files folder imageHelpers (1 file)
Files folder imageIntegration (1 file)
Files folder imageUnit (7 files)

  Files folder image Files (50)  /  tests  /  Helpers  
File Role Description
  Plain text file Reflection.php Class Class source

  Files folder image Files (50)  /  tests  /  Integration  
File Role Description
  Plain text file addContainersToPodTest.php Class Class source

  Files folder image Files (50)  /  tests  /  Unit  
File Role Description
  Plain text file ContainerTest.php Class Class source
  Plain text file LabelsTest.php Class Class source
  Plain text file NodeLabelsTest.php Class Class source
  Plain text file PodAnnotationsTest.php Class Class source
  Plain text file PodLabelsTest.php Class Class source
  Plain text file PodTest.php Class Class source
  Plain text file ServiceAccountAnnotationsTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:28
This week:0
All time:11,158
This week:455Up
User Comments (1)
Thats a very good class, very very organized, congratulations !
1 year ago (José Filipe Lopes Santos)
70%StarStarStarStar