core

package
v0.67.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 29, 2024 License: Apache-2.0 Imports: 35 Imported by: 0

README

Kubernetes service

This service is *k8s.io/client-go/kubernetes.Clientset proxy

It defines helper method to deal with basic operation in a friendly way.

To check all supported method run

     endly -s='kubernetes'

or to check service contract endly -s='kubernetes' -a=ACTION

     endly -s='kubernetes' -a=get

Usage

Get k8s resource info

endly kubernetes:get kind=pod
 endly kubernetes:get kind=pod labelSelector="run=load-balancer-example"
endly kubernetes:get kind=endpoints 

Create resources

Create resource(s) with create API method call.

  1. Create with external resource:

    endly -run='kubernetes:create' URL=test/deployment.yaml
    
  2. Create workflow

    pipeline:
      createNginx:
        action: kubernetes:create
        apiVersion: apps/v1
        kind: Deployment
        metadata:
          labels:
            run: nginx
          name: nginx
        spec:
          replicas: 1
          selector:
            matchLabels:
              run: nginx
          template:
            metadata:
              labels:
                run: nginx
            spec:
              containers:
                - image: nginx
                  imagePullPolicy: Always
                  name: nginx
              restartPolicy: Always
    

Apply - create or patch resources

Create resource(s) with create API method call or apply patch.

  1. Apply with external resource:

    endly -run='kubernetes:apply' URL=test/deployment.yaml
    
  2. Apply workflow

    pipeline:
      applyNginx:
        action: kubernetes:apply
        URL: someURL.yaml
    

Run - crete resource from template

  1. Start a single instance (apps/v1.Deployment template)
  • Start a single instance of nginx cli.
    endly -run='kubernetes:run' name=nginx image=nginx  port=80 
    
  • Start a single instance of hazelcast, expose port 5701 and set environment variables "DNS_DOMAIN=cluster" and "POD_NAMESPACE=default" in the container.
    endly -run='kubernetes:run' name=hazelcast image=hazelcast port=5701 env.DNS_DOMAIN=cluster env.POD_NAMESPACE=default
    
  • Start a single instance of hazelcast and set labels "app=hazelcast" and "env=prod" in the container.
    pipeline:
      runHazelcast:
        action: kubernetes:run
        name: hazelcast
        image: hazelcast
        labels:
          app: hazelcast
          env: prod
    
  1. Start a replicated instance (apps/v1.Deployment template)
    pipeline:
      runNgix:
        action: kubernetes:run
        name: nginx
        image: nginx
        replicas: 5
    
  2. Start single pod instance (v1.Pod template)
    pipeline:
      runPod:
        action: kubernetes:run
        name: nginx
        image: nginx
        restartPolicy: Never
    
  3. Start the perl container to compute π to 2000 places and print it out. (batch/v1.Job template)
    pipeline:
      runJob:
        action: kubernetes:run
        name: pi
        image: perl
        restartPolicy: OnFailure
        commands:
          - "perl -Mbignum=bpi -wle 'print bpi(2000)'"
    
  4. Start the cron job to compute π to 2000 places and print it out every 1 minutes.
    pipeline:
      runJob:
        action: kubernetes:run
        name: pi
        image: perl
        schedule: 0/1 * * * ?
        commands:
          - "perl -Mbignum=bpi -wle 'print bpi(2000)'"
    

Expose resources

Expose resource(s) port via service port.

  1. Create a service for a explicit resource and port
    endly -run=kubernetes:expose resource=Deployment/nginx port=8080 targetPort=80 type=NodePort
    
  2. pipeline:
      applyNginx:
        action: kubernetes:expose
        name: myService
        resource: Deployment/myApp
    

Delete resources

  1. Delete resource
    endly -run='kubernetes:delete' kind=pod name=myPod
    
  2. Delete resources
    endly -run='kubernetes:delete' kind='*' name=myPod
    
  3. Delete resource from file
    endly -run='kubernetes:delete' URL=resources.yaml
    

Config Maps

  1. Creating config maps with yaml resource file

    pipeline:
      createConfig:
        action: kubernetes:create
        URL: config.yaml
        expand: true
    
  2. Creating config maps

    pipeline:
      createConfig:
        action: kubernetes:create
        kind: ConfigMap
        apiVersion: v1
        metadata:
          name: examplecfg
        data:
          config.property.1: value1
          config.property.2: value2
          config.properties: |-
            property.1=value-1
            property.2=value-2
            property.3=value-3
        binaryData:
          foo: L3Jvb3QvMTAw
    
  3. Creating config maps for folder/URL

    pipeline:
      createConfig:
        action: kubernetes:create
        kind: ConfigMap
        apiVersion: v1
        metadata:
          name: mycfg
        data: $AssetsToMap('config/')
        binaryData: $BinaryAssetsToMap('config/bin')
    

Secrets

  1. Creating secrets from literals
    init:
      username: $Cat(somefile.txt)
      password: dev
    pipeline:
      setSecrets:
        action: kubernetes:apply
        apiVersion: v1
        kind: Secret
        metadata:
          name: my-secrets
        type: Opaque
        data:
          username: $Base64Encode($username)
          password: $Base64Encode($password)    
    
  2. Creating secrets with endly secrets
    init:
      devSecrets: $secrets.dev
    pipeline:
      info:
        action: print
        message: $devSecrets.Data
    
      setSecrets:
        action: kubernetes:apply
        apiVersion: v1
        kind: Secret
        metadata:
          name: dev-secrets
        type: Opaque
        data:
          dev.json: $Base64Encode($devSecrets.Data)
          username: $Base64Encode($devSecrets.Username)
          password: $Base64Encode($devSecrets.Password)
    
    
  3. Testing secrets without pipeline
    • endly kubernetes:get secrets kind=secret name=dev-secrets
    • endly kubernetes:get secrets kind=secret
    • endly kubernetes:apply url=dev-secrets.yaml

Forwarding ports

  1. Forwarding port for service in interactive mode (-m)
    pipeline:
      setup:
        action: kubernetes:forwardPorts
        kind: service
        name: mysql
        ports:
          - 3306:3306
    
  2. Forwarding port for pod
    pipeline:
      setup:
        action: kubernetes:forwardPorts
        kind: pod
        name: mysql-648d67bf7c-prfgs
        ports:
          - 3306:3306
    

Global contract parameters

  • context
  • namespace
  • kubeconfig
  • masterurl

Documentation

Index

Constants

View Source
const (
	RunV1GeneratorName            = "run/v1"
	RunPodV1GeneratorName         = "run-pod/v1"
	DeploymentAppsV1GeneratorName = "deployment/apps.v1"
	JobV1GeneratorName            = "job/v1"
	CronJobV1Beta1GeneratorName   = "cronjob/v1beta1"
)
View Source
const (
	//ServiceID Kubernetes service ID.
	ServiceID = "kubernetes"
)
View Source
const (
	ServiceV1GeneratorName = "service/v1"
)

Variables

This section is empty.

Functions

func GenerateRequest added in v0.31.0

func GenerateRequest(name string, templates map[string]string, templateParams interface{}, handler func(meta *ResourceMeta, data map[string]interface{}) error) error

GenerateRequest converts request with provided template and call handler with meta and request data.

func IsIntStrEmpty added in v0.31.0

func IsIntStrEmpty(value intstr.IntOrString) bool

func New

func New() *service

New creates a new service

func ProcessResource

func ProcessResource(context *endly.Context, expand bool, resource *url.Resource, reverse bool, handler func(meta *ResourceMeta, data map[string]interface{}) error) error

func ShortItemInfo added in v0.31.0

func ShortItemInfo(kind string, item map[string]interface{}) map[string]interface{}

ShortResourceResponse returns short info

Types

type ApplyRequest

type ApplyRequest struct {
	*url.Resource
	*ResourceMeta
	Expand bool `description:"flag to expand resource with $ expression"`
}

ApplyRequest represents apply request

func (*ApplyRequest) Init added in v0.33.0

func (r *ApplyRequest) Init() error

Init initializes request

type ApplyResponse added in v0.31.0

type ApplyResponse ResourceInfoResponse

ApplyResponse represents apply response

type CreateRequest

type CreateRequest struct {
	*ResourceMeta
	*url.Resource
	Expand bool `description:"flag to expand resource with $ expression"`
}

CreateRequest represents create request

func (*CreateRequest) Init added in v0.31.0

func (r *CreateRequest) Init() (err error)

type CreateResponse

type CreateResponse ResourceInfoResponse

CreateResponse represents create response

type DeleteRequest added in v0.31.0

type DeleteRequest struct {
	Name            string
	LabelSelector   string
	metav1.TypeMeta `json:",inline"`
	*url.Resource
	TimeoutMs int
}

DeleteRequest represents delete response

func (*DeleteRequest) AsGetRequest added in v0.31.0

func (r *DeleteRequest) AsGetRequest() *GetRequest

AsGetRequest returns get request

func (*DeleteRequest) Init added in v0.31.0

func (r *DeleteRequest) Init() (err error)

type DeleteResponse added in v0.31.0

type DeleteResponse ResourceInfoResponse

DeleteResponse represents delete response

type ExposeRequest added in v0.31.0

type ExposeRequest struct {
	Resource     string `description:"a target resource name to be exposed"`
	ResourceKind string `description:"optional target resource kind"`

	Name           string      `description:"metadata.name"`
	Protocol       v1.Protocol `description:"spec.ports[].protocol"`
	Port           int32       `description:"spec.ports[].port: expose port"`
	TargetPort     string      `description:"spec.ports[].targetPort: name or number for the port on the container that the service should direct traffic to"`
	Type           string      `description:"spec.type: for this service: ClusterIP, NodePort, LoadBalancer, or ExternalName"`
	LoadBalancerIP string      `description:"spec.loadBalancerIP: IP to assign to the LoadBalancer"`

	Labels              map[string]string `description:"spec.labels"`
	ExternalIPs         string            `description:"spec.ExternalIPs"`
	SessionAffinity     string            `description:"spec.SessionAffinity: if non empty: 'None', 'ClientIP'"`
	ClusterIP           string            `` /* 136-byte string literal not displayed */
	ExternalName        string            `description:"spec.externalName"`
	HealthCheckNodePort int               `description:"spec.HealthCheckNodePort"`
	// contains filtered or unexported fields
}

ExposeRequest represent expose request

func (*ExposeRequest) Init added in v0.31.0

func (r *ExposeRequest) Init() error

Init initialises request

func (*ExposeRequest) Validate added in v0.31.0

func (r *ExposeRequest) Validate() error

Validate checks if request is valid

type ExposeResponse added in v0.31.0

type ExposeResponse ResourceInfoResponse

ExposeResponse represent expose response

type ExposeTemplateParams added in v0.31.0

type ExposeTemplateParams struct {
	*ExposeRequest
	Ports                 []v1.ServicePort
	Selector              map[string]string
	SessionAffinityConfig *v1.SessionAffinityConfig
}

func NewExposeTemplateParams added in v0.31.0

func NewExposeTemplateParams(source *ResourceInfo, request *ExposeRequest) (*ExposeTemplateParams, error)

func (*ExposeTemplateParams) Apply added in v0.31.0

func (p *ExposeTemplateParams) Apply(source *ResourceInfo) error

type ForwardPortsRequest added in v0.33.0

type ForwardPortsRequest struct {
	Name            string `description:"resource name"`
	LabelSelector   string `json:"selector" yaml:"selector" description:"selector for matching pod or resource with pod spec"`
	metav1.TypeMeta `json:",inline"`
	Ports           []string `description:"ports to forward in dest:source or just port"`
	TimeoutMs       int      `description:"maximum wait time for pod getting ready"`
}

ForwardPortsRequest represents forward port request

func (*ForwardPortsRequest) AsGetRequest added in v0.33.0

func (r *ForwardPortsRequest) AsGetRequest() (*GetRequest, error)

AsGetRequest returns get request

func (*ForwardPortsRequest) Init added in v0.33.0

func (r *ForwardPortsRequest) Init() error

Validate checks if request is valid

func (*ForwardPortsRequest) Validate added in v0.33.0

func (r *ForwardPortsRequest) Validate() error

Validate checks if request is valid

type ForwardPortsResponse added in v0.33.0

type ForwardPortsResponse struct {
	Name string
}

ForwardPortsRequest represents forward port response

type GetRequest

type GetRequest struct {
	Name string
	metav1.ListOptions
	Describe bool `description:"describe flag control output"`
	// contains filtered or unexported fields
}

GetRequest represents get request

func (*GetRequest) Init

func (r *GetRequest) Init() (err error)

func (*GetRequest) Validate added in v0.31.0

func (r *GetRequest) Validate() (err error)

type GetResponse

type GetResponse ResourceInfoResponse

GetResponse represents get response

type ResourceCondition added in v0.31.0

type ResourceCondition struct {
	Type    string
	Status  string
	Reason  string
	Message string
}

ResourceCondition contains details for the current condition

type ResourceInfo added in v0.31.0

type ResourceInfo struct {
	metav1.TypeMeta   `yaml:",inline"`
	metav1.ObjectMeta `yaml:"metadata"`
	Spec              interface{}
	Status            interface{}
	Raw               interface{} `yaml:"-"`
}

ResourceInfo represents generic resource info

func (*ResourceInfo) ContainerPorts added in v0.31.0

func (i *ResourceInfo) ContainerPorts() []v1.ContainerPort

func (*ResourceInfo) Containers added in v0.31.0

func (i *ResourceInfo) Containers() []v1.Container

func (*ResourceInfo) IsReady added in v0.31.0

func (i *ResourceInfo) IsReady() bool

func (*ResourceInfo) ResourceStatus added in v0.31.0

func (i *ResourceInfo) ResourceStatus() *ResourceStatus

type ResourceInfoResponse added in v0.31.0

type ResourceInfoResponse struct {
	Items []*ResourceInfo
}

ResourceInfoResponse represents info response

type ResourceMeta added in v0.31.0

type ResourceMeta struct {
	metav1.TypeMeta
	Metadata metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
}

ResourceMeta represents a k8 resource meta

type ResourcePatch added in v0.31.0

type ResourcePatch struct {
	metav1.TypeMeta `json:",inline"`
	Name            string
	Pt              types.PatchType
	Data            []byte
	HasChanged      bool
}

ResourcePatch represents resource patch

func NewResourcePatch added in v0.31.0

func NewResourcePatch(meta *ResourceMeta, original, target interface{}) (*ResourcePatch, error)

NewResourcePatch returns a new resource patch

type ResourceStatus added in v0.31.0

type ResourceStatus struct {
	Conditions               []ResourceCondition
	PublishNotReadyAddresses *bool
}

type ResourcesMetaInfo added in v0.31.0

type ResourcesMetaInfo struct {
	*ResourceMeta `json:"metadata,omitempty" yaml:"metadata,omitempty"`
	Items         []*ResourceMeta `json:"items,omitempty" yaml:"items,omitempty"`
}

ResourcesMetaInfo represents resource meta info

func ToResourceMetas added in v0.31.0

func ToResourceMetas(items []*ResourceInfo) *ResourcesMetaInfo

ToResourceMetas converts *ResourceInfo slice to []*ResourceMeta

type RunRequest added in v0.31.0

type RunRequest struct {
	Expose          bool
	Replicas        int
	Template        string
	Schedule        string
	Labels          map[string]string
	Name            string             `description:"metadata.name"`
	RestartPolicy   core.RestartPolicy `description:"spec.restartPolicy"`
	DNSPolicy       v1.DNSPolicy       `description:"spec.dNSPolicy"`
	ServiceAccount  string             `description:"spec.serviceAccountName"`
	ImagePullPolicy core.PullPolicy    `description:"spec.containers[].imagePullPolicy"`
	Image           string             `description:"spec.containers[].image"`
	Commands        []string           `description:"spec.containers[].commands"`
	Args            []string           `description:"spec.containers[].args"`
	Env             map[string]string  `description:"spec.containers[].env"`
	Limits          map[string]string  `description:"spec.containers[].resources.limits"`
	Requests        map[string]string  `description:"spec.containers[].resources.requests"`
	Port            int                `description:"spec.containers[].ports[].containerPort"`
	HostPort        int                `description:"spec.containers[].ports[].hostPort"`
}

RunRequest represents run request

func (*RunRequest) Init added in v0.31.0

func (r *RunRequest) Init() error

func (*RunRequest) Validate added in v0.31.0

func (r *RunRequest) Validate() error

type RunResponse added in v0.31.0

type RunResponse ResourceInfoResponse

RunResponse represents run response

type RunTemplateParams added in v0.31.0

type RunTemplateParams struct {
	*RunRequest
	Ports         []*v1.ContainerPort
	Resources     []*v1.ResourceRequirements
	LabelSelector *metav1.LabelSelector // {MatchLabels: labels}
	Envs          []v1.EnvVar
}

RunTemplateParams represents run template parameters

func NewRunTemplateParams added in v0.31.0

func NewRunTemplateParams(request *RunRequest) (*RunTemplateParams, error)

NewRunTemplateParams create a new run template parameters for supplied run request

type ShortResourceResponse added in v0.31.0

type ShortResourceResponse struct {
	Items []interface{}
}

ShortResourceResponse represents resource meta info

func NewShortResourceResponse added in v0.31.0

func NewShortResourceResponse(response ResourceInfoResponse) *ShortResourceResponse

NewShortResourceResponse create short response

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL