conversion

package
v1.17.3 Latest Latest
Warning

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

Go to latest
Published: May 20, 2026 License: Apache-2.0 Imports: 23 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultSettings = &WebhookSettings{
	Settings: server.Settings{
		ServerCertPath: "/conversion-certs/tls.crt",
		ServerKeyPath:  "/conversion-certs/tls.key",
		ServiceName:    "shell-operator-conversion-svc",
		ListenAddr:     "0.0.0.0",
		ListenPort:     "9681",
	},
	CAPath: "/conversion-certs/ca.crt",
}

DefaultSettings holds the active conversion-webhook server settings. It is populated by InitFromSettings during operator initialization. Tests may use it directly; in production always call InitFromSettings first.

View Source
var SupportedConversionReviewVersions = []string{"v1", "v1beta1"}

Functions

func ExtractAPIVersions

func ExtractAPIVersions(objs []runtime.RawExtension) []string

func InitFromSettings added in v1.17.0

func InitFromSettings(s WebhookSettings)

InitFromSettings replaces DefaultSettings with values derived from cfg. Call this once during operator initialization, after all configuration sources (env vars and CLI flags) have been merged into cfg.

func VersionsMatched

func VersionsMatched(v0, v1 string) bool

VersionsMatched when: - ver0 equals to ver1 - ver0 is short and ver1 is full and short ver1 is equals to ver0 - ver0 is full and ver1 is short and short ver0 is equals to ver1 Note that ver0 and ver1 with different groups are not matched (e.g. stable/v1 and unstable/v1)

Types

type Chain

type Chain struct {
	// A cache of calculated paths. A key is an arbitrary path and value is a sequence of base paths.
	PathsCache map[Rule][]Rule
	// An index to search for base paths by given From and To.
	BaseFromToIndex map[string]map[string]struct{}
}

func (Chain) HasTargetVersion

func (c Chain) HasTargetVersion(target string) bool

HasTargetVersion returns true if there is a base path leading to an input version.

func (Chain) NextRules

func (c Chain) NextRules(fromVer string) []Rule

NextRules finds all base paths in BaseFromToIndex that starts from an input version.

func (*Chain) Put

func (c *Chain) Put(rule Rule)

Put adds a base path defined by Rule to a cache and updates a FromTo index.

func (Chain) RulesWithSimilarFromVersion

func (c Chain) RulesWithSimilarFromVersion(rule Rule) []Rule

RulesWithFromVersion returns all rules in PathsCache that has similar FromVersion as the input rule.

func (Chain) SearchPathForRule

func (c Chain) SearchPathForRule(rule Rule) []Rule

SearchPathForRule returns an array of base paths for an arbitrary rule.

type ChainStorage

type ChainStorage struct {
	Chains map[string]*Chain
}

func NewChainStorage

func NewChainStorage() *ChainStorage

func (ChainStorage) FindConversionChain

func (cs ChainStorage) FindConversionChain(crdName string, rule Rule) []Rule

Calculations FindConversionChain returns an array of ConverionsRules that should be

func (ChainStorage) Get

func (cs ChainStorage) Get(crdName string) *Chain

Access a Chain by CRD full name (e.g. crontab.stable.example.com)

type CrdClientConfig

type CrdClientConfig struct {
	KubeClient  *klient.Client
	CrdName     string
	Namespace   string
	ServiceName string
	Path        string
	CABundle    []byte
}

A clientConfig for a particular CRD.

func (*CrdClientConfig) Update

func (c *CrdClientConfig) Update(ctx context.Context) error

type EventHandlerFn

type EventHandlerFn func(ctx context.Context, cdrName string, request *v1.ConversionRequest) (*Response, error)

type Option added in v1.14.3

type Option func(manager *WebhookManager)

func WithLogger added in v1.14.3

func WithLogger(logger *log.Logger) Option

type Response

type Response struct {
	FailedMessage    string                 `json:"failedMessage"`
	ConvertedObjects []runtime.RawExtension `json:"convertedObjects,omitempty"`
}

Response is a holder of the conversion hook response.

Unlike ConverionsResponse, only one filed (FailedMessage) is used to determine success or fail:

Response is Success if FailedMessage is empty:

"result": {
  "status": "Success"
},

Response is Failed:

"result": {
  "status": "Failed",
  "message": FailedMessage
}

ConvertedObjects: # Objects must match the order of request.objects, and have apiVersion set to <request.desiredAPIVersion>. # kind, metadata.uid, metadata.name, and metadata.namespace fields must not be changed by the webhook. # metadata.labels and metadata.annotations fields may be changed by the webhook. # All other changes to metadata fields by the webhook are ignored.

func ResponseFromBytes

func ResponseFromBytes(data []byte) (*Response, error)

func ResponseFromFile

func ResponseFromFile(filePath string) (*Response, error)

func ResponseFromReader

func ResponseFromReader(r io.Reader) (*Response, error)

func (*Response) Dump

func (r *Response) Dump() string

type Rule

type Rule struct {
	FromVersion string `json:"fromVersion"`
	ToVersion   string `json:"toVersion"`
}

func (Rule) ShortFromVersion

func (r Rule) ShortFromVersion() string

func (Rule) ShortToVersion

func (r Rule) ShortToVersion() string

func (Rule) String

func (r Rule) String() string

type WebhookConfig

type WebhookConfig struct {
	Rules    []Rule
	CrdName  string // This name is used as a suffix to create different URLs for clientConfig in CRD.
	Metadata struct {
		Name         string
		DebugName    string
		LogLabels    map[string]string
		MetricLabels map[string]string
	}
}

WebhookConfig

type WebhookHandler

type WebhookHandler struct {
	Manager *WebhookManager
	Router  chi.Router

	Logger *log.Logger
}

func NewWebhookHandler

func NewWebhookHandler(logger *log.Logger) *WebhookHandler

type WebhookManager

type WebhookManager struct {
	KubeClient *klient.Client

	EventHandlerFn EventHandlerFn
	Settings       *WebhookSettings
	Namespace      string

	Server        *server.WebhookServer
	ClientConfigs map[string]*CrdClientConfig
	Handler       *WebhookHandler

	Logger *log.Logger
}

WebhookManager is a public interface to be used from operator.go.

No dynamic configuration for now. The steps are: - Init():

  • Create a router to distinguish conversion requests between CRDs
  • Create a handler to handle ConversionReview
  • Create a server that listens for Kubernetes requests
  • Call AddWebhook() to register a CRD name in conversion bindings in hooks

- Start():

  • Start server loop.
  • Update clientConfig in each registered CRD.

func NewWebhookManager

func NewWebhookManager(opts ...Option) *WebhookManager

func (*WebhookManager) AddWebhook

func (m *WebhookManager) AddWebhook(webhook *WebhookConfig)

func (*WebhookManager) Init

func (m *WebhookManager) Init() error

Init creates dependencies

func (*WebhookManager) Start

func (m *WebhookManager) Start() error

Start webhook server and update spec.conversion in CRDs.

type WebhookSettings

type WebhookSettings struct {
	server.Settings
	CAPath   string
	CABundle []byte
}

Jump to

Keyboard shortcuts

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