k8s

package module
v2.0.7 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2024 License: MIT Imports: 18 Imported by: 2

README

Note

Defines the key name of specific fields

GoMicro needs to cooperate with the following fields to run properly on kubernetes:

  • gomicro-service-id: define the ID of the service
  • gomicro-service-app: define the name of the service
  • gomicro-service-version: define the version of the service
  • gomicro-service-metadata: define the metadata of the service
  • gomicro-service-protocols: define the protocols of the service

Example Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: shop_api
  labels:
    app: shop_api
spec:
  replicas: 2
  selector:
    matchLabels:
      app: shop_api
  template:
    metadata:
      labels:
        app: shop_api
        gomicro-service-id: "56991810-c77f-4a95-8190-393efa9c1a61"
        gomicro-service-app: "shop_api"
        gomicro-service-version: "v1.0.0"
      annotations:
        gomicro-service-protocols: |
          {"8080": "http"}
        gomicro-service-metadata: |
          {"region": "sh", "zone": "sh001", "cluster": "pd"}
    spec:
      containers:
        - name: shop_api
          image: shop_api:1.0.0
          ports:
            - containerPort: 8080

Documentation

Index

Constants

View Source
const (
	// LabelsKeyServiceID is used to define the ID of the service
	LabelsKeyServiceID = "gomicro-service-id"

	// LabelsKeyServiceName is used to define the name of the service
	LabelsKeyServiceName = "gomicro-service-app"

	// LabelsKeyServiceVersion is used to define the version of the service
	LabelsKeyServiceVersion = "gomicro-service-version"

	// AnnotationsKeyMetadata is used to define the metadata of the service
	AnnotationsKeyMetadata = "gomicro-service-metadata"

	// AnnotationsKeyProtocolMap is used to define the protocols of the service
	// Through the value of this field, GoMicro can obtain the application layer protocol corresponding to the port
	// Example value: {"8080": "http", "9090": "grpc"}
	AnnotationsKeyProtocolMap = "gomicro-service-protocols"
)
View Source
const ServiceAccountNamespacePath = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"

ServiceAccountNamespacePath defines the location of the namespace file

Variables

View Source
var ErrIteratorClosed = errors.New("iterator closed")

ErrIteratorClosed defines the error that the iterator is closed

Functions

func GetNamespace

func GetNamespace() string

GetNamespace is used to get the namespace of the Pod where the current container is located

func GetPodName

func GetPodName() string

GetPodName is used to get the name of the Pod where the current container is located

func LoadNamespace

func LoadNamespace() string

LoadNamespace is used to get the current namespace from the file

Types

type ErrorHandleResource

type ErrorHandleResource struct {
	Namespace string
	Name      string
	Reason    error
}

ErrorHandleResource defines the error that cannot handle K8S resources normally

func (*ErrorHandleResource) Error

func (err *ErrorHandleResource) Error() string

Error implements the error interface

type Iterator

type Iterator struct {
	// contains filtered or unexported fields
}

Iterator performs the conversion from channel to iterator It reads the latest changes from the `chan []*registry.ServiceInstance` And the outside can sense the closure of Iterator through stopCh

func NewIterator

func NewIterator(channel chan []*registry.ServiceInstance, stopCh chan struct{}) *Iterator

NewIterator is used to initialize Iterator

func (*Iterator) Next

func (iter *Iterator) Next() ([]*registry.ServiceInstance, error)

Next will block until ServiceInstance changes

func (*Iterator) Stop

func (iter *Iterator) Stop() error

Stop is used to close the iterator

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

The Registry simply implements service discovery based on Kubernetes

func NewRegistry

func NewRegistry(clientSet *kubernetes.Clientset) *Registry

NewRegistry is used to initialize the Registry

func (*Registry) Close

func (s *Registry) Close()

Close is used to close the Registry After closing, any callbacks generated by Watch will not be executed

func (*Registry) Deregister

func (s *Registry) Deregister(ctx context.Context, _ *registry.ServiceInstance) error

Deregister the registration.

func (*Registry) GetService

func (s *Registry) GetService(ctx context.Context, name string) ([]*registry.ServiceInstance, error)

GetService return the service instances in memory according to the service name.

func (*Registry) Register

func (s *Registry) Register(ctx context.Context, service *registry.ServiceInstance) error

Register is used to register services

func (*Registry) Start

func (s *Registry) Start()

Start is used to start the Registry It is non-blocking

func (*Registry) Watch

func (s *Registry) Watch(ctx context.Context, name string) (registry.Watcher, error)

Watch creates a watcher according to the service name.

Jump to

Keyboard shortcuts

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