utils

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2024 License: Apache-2.0 Imports: 29 Imported by: 6

Documentation

Index

Constants

View Source
const (
	// ImplementationTypeKongGateway indicates an implementation backed by Kong Gateway.
	ImplementationTypeKongGateway = "kong-gateway"
)

Variables

View Source
var (
	Kong140Version = semver.MustParse("1.4.0")
	Kong300Version = semver.MustParse("3.0.0")
	Kong340Version = semver.MustParse("3.4.0")
)
View Source
var ErrorConsumerGroupUpgrade = errors.New(
	"a rate-limiting-advanced plugin with config.consumer_groups\n" +
		"and/or config.enforce_consumer_groups was found. Please use Consumer Groups scoped\n" +
		"Plugins when running against Kong Enterprise 3.4.0 and above.\n\n" +
		"Check https://docs.konghq.com/gateway/latest/kong-enterprise/consumer-groups/ for more information",
)
View Source
var UpgradeMessage = "Please upgrade your configuration to account for 3.0\n" +
	"breaking changes using the following command:\n\n" +
	"deck convert --from kong-gateway-2.x --to kong-gateway-3.x\n\n" +
	"This command performs the following changes:\n" +
	"  - upgrade the `_format_version` value to `3.0`\n" +
	"  - add the `~` prefix to all routes' paths containing a regex-pattern\n\n" +
	"These changes may not be correct or exhaustive enough.\n" +
	"It is strongly recommended to perform a manual audit\n" +
	"of the updated configuration file before applying\n" +
	"the configuration in production. Incorrect changes will result in\n" +
	"unintended traffic routing by Kong Gateway.\n\n" +

	"For more information about this and related changes,\n" +
	"please visit: https://docs.konghq.com/deck/latest/3.0-upgrade\n\n"

Functions

func AddExtToFilename

func AddExtToFilename(filename, ext string) string

func CallGetAll

func CallGetAll(obj interface{}) (reflect.Value, error)

func CleanAddress

func CleanAddress(address string) string

CleanAddress removes trailling / from a URL.

func CleanKongVersion

func CleanKongVersion(version string) (string, error)

CleanKongVersion takes a version of Kong and returns back a string in the form of `/major.minor` version. There are various dashes and dots and other descriptors in Kong version strings, which has often created confusion in code and incorrect parsing, and hence this function does not return the patch version (on which shouldn't rely on anyways).

func ConfigFilesInDir

func ConfigFilesInDir(dir string) ([]string, error)

ConfigFilesInDir traverses the directory rooted at dir and returns all the files with a case-insensitive extension of `yml` or `yaml`.

func Confirm

func Confirm(message string) (bool, error)

Confirm prompts a user for a confirmation with message and returns true with no error if input is "yes" or "y" (case-insensitive), otherwise false.

func ConfirmFileOverwrite

func ConfirmFileOverwrite(filename string, ext string, assumeYes bool) (bool, error)

ConfirmFileOverwrite is a helper function to determine whether or not the program should truncate and overwrite a file given its name and extension. If the file doesn't already exist in the filesystem, then this will return true, otherwise it will prompt the user for confirmation.

func Empty

func Empty(s *string) bool

Empty checks if a string referenced by s or s itself is empty.

func FilenameToName

func FilenameToName(filename string) string

FilenameToName (partially) reverses NameToFilename, replacing all URL-encoded path separator characters with the path separator character. It does not re-add a leading separator, because there is no way to know if that separator was included originally, and only some names (document paths) typically include one.

func GetConsumerGroupReference

func GetConsumerGroupReference(c kong.ConsumerGroup) *kong.ConsumerGroup

GetConsumerGroupReference returns a name+ID only copy of the input consumer-group, for use in references from other objects

func GetConsumerReference

func GetConsumerReference(c kong.Consumer) *kong.Consumer

GetConsumerReference returns a username+ID only copy of the input consumer, for use in references from other objects

func GetKongClient

func GetKongClient(opt KongClientConfig) (*kong.Client, error)

GetKongClient returns a Kong client

func GetKonnectClient

func GetKonnectClient(httpClient *http.Client, config KonnectConfig) (*konnect.Client,
	error,
)

func GetRouteReference

func GetRouteReference(r kong.Route) *kong.Route

GetRouteReference returns a name+ID only copy of the input route, for use in references from other objects

func GetServiceReference

func GetServiceReference(s kong.Service) *kong.Service

GetServiceReference returns a name+ID only copy of the input service, for use in references from other objects

func HTTPClient

func HTTPClient() *http.Client

HTTPClient returns a new Go stdlib's net/http.Client with sane default timeouts.

func HTTPClientWithTLSConfig added in v1.5.0

func HTTPClientWithTLSConfig(opt TLSConfig) (*http.Client, error)

func HasPathsWithRegex300AndAbove

func HasPathsWithRegex300AndAbove(route kong.Route) bool

HasPathsWithRegex300AndAbove checks routes' paths format and returns true if these math a regex-pattern without a '~' prefix.

func IsPathRegexLike

func IsPathRegexLike(path string) bool

IsPathRegexLike checks if a path string contains a regex pattern.

func MergeTags

func MergeTags(obj interface{}, tags []string) error

MergeTags merges Tags in the object with tags.

func MustMergeTags

func MustMergeTags(obj interface{}, tags []string)

MustMergeTags is same as MergeTags but panics if there is an error.

func MustRemoveTags

func MustRemoveTags(obj interface{}, tags []string)

MustRemoveTags is same as RemoveTags but panics if there is an error.

func NameToFilename

func NameToFilename(name string) string

NameToFilename clears path separators from strings. Some entity names in Kong and Konnect allow path directory separators. Some decK operations write files using entity names, which is not compatible with names that contain path separators. NameToFilename strips leading separator characters and replaces other instances of the separator with its URL-encoded representation.

func ParseKongVersion

func ParseKongVersion(version string) (semver.Version, error)

ParseKongVersion takes a version string from the Gateway and turns it into a semver-compliant version to be used for comparison across the code.

func PrintRouteRegexWarning

func PrintRouteRegexWarning(unsupportedRoutes []string)

PrintRouteRegexWarning prints out a warning about 3.x routes' path usage.

func RemoveDuplicates

func RemoveDuplicates(slice *[]string)

RemoveDuplicates removes duplicated elements from a slice.

func RemoveTags

func RemoveTags(obj interface{}, tags []string) error

RemoveTags removes tags from the Tags in obj.

func SendAnalytics

func SendAnalytics(cmd, deckVersion, kongVersion, mode string) error

func UUID

func UUID() string

UUID will generate a random v4 unique identifier

func WorkspaceExists

func WorkspaceExists(ctx context.Context, client *kong.Client) (bool, error)

func ZeroOutField

func ZeroOutField(obj interface{}, field string)

func ZeroOutID

func ZeroOutID(obj interface{}, altName *string, withID bool)

func ZeroOutTimestamps

func ZeroOutTimestamps(obj interface{})

Types

type AtomicInt32Counter

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

func (*AtomicInt32Counter) Count

func (a *AtomicInt32Counter) Count() int32

func (*AtomicInt32Counter) Increment

func (a *AtomicInt32Counter) Increment(delta int32)

type Defaulter

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

Defaulter registers types and fills in struct fields with default values.

func GetDefaulter

func GetDefaulter(ctx context.Context, opts DefaulterOpts) (*Defaulter, error)

GetDefaulter returns a Defaulter object to be used to set defaults on Kong entities. The order of precedence is as follow, from higher to lower:

1. values set in the state file 2. values set in the {_info: defaults:} object in the state file 3. hardcoded defaults under utils/constants.go (Konnect-only)

func NewDefaulter

func NewDefaulter() *Defaulter

NewDefaulter initializes a Defaulter with empty entities.

func (*Defaulter) MustSet

func (d *Defaulter) MustSet(arg interface{})

MustSet is like Set but panics if there is an error.

func (*Defaulter) Register

func (d *Defaulter) Register(def interface{}) error

Register registers a type and it's default value. The default value is passed in and the type is inferred from the default value.

func (*Defaulter) Set

func (d *Defaulter) Set(arg interface{}) error

Set fills in default values in a struct of a registered type.

type DefaulterOpts

type DefaulterOpts struct {
	KongDefaults           interface{}
	DisableDynamicDefaults bool
	IsKonnect              bool
	Client                 *kong.Client
}

type ErrArray

type ErrArray struct {
	Errors []error
}

ErrArray holds an array of errors.

func (ErrArray) Error

func (e ErrArray) Error() string

Error returns a pretty string of errors present.

func (ErrArray) ErrorList

func (e ErrArray) ErrorList() []string

type KongClientConfig

type KongClientConfig struct {
	Address   string
	Workspace string

	Debug bool

	SkipWorkspaceCrud bool

	Headers []string

	HTTPClient *http.Client

	Timeout int

	CookieJarPath string

	// whether or not the client should retry on 429s
	Retryable bool

	TLSConfig TLSConfig
}

KongClientConfig holds config details to use to talk to a Kong server.

func (*KongClientConfig) ForWorkspace

func (kc *KongClientConfig) ForWorkspace(name string) KongClientConfig

ForWorkspace returns a copy of KongClientConfig that produces a KongClient for the workspace specified by argument.

type KongRawState

type KongRawState struct {
	Services []*kong.Service
	Routes   []*kong.Route

	Plugins []*kong.Plugin

	Upstreams []*kong.Upstream
	Targets   []*kong.Target

	Certificates   []*kong.Certificate
	SNIs           []*kong.SNI
	CACertificates []*kong.CACertificate

	Consumers      []*kong.Consumer
	ConsumerGroups []*kong.ConsumerGroupObject
	CustomEntities []*custom.Entity

	Vaults   []*kong.Vault
	Licenses []*kong.License

	KeyAuths    []*kong.KeyAuth
	HMACAuths   []*kong.HMACAuth
	JWTAuths    []*kong.JWTAuth
	BasicAuths  []*kong.BasicAuth
	ACLGroups   []*kong.ACLGroup
	Oauth2Creds []*kong.Oauth2Credential
	MTLSAuths   []*kong.MTLSAuth

	RBACRoles               []*kong.RBACRole
	RBACEndpointPermissions []*kong.RBACEndpointPermission
}

KongRawState contains all of Kong Data

type KonnectConfig

type KonnectConfig struct {
	Email    string
	Password string
	Token    string
	Debug    bool

	Address string

	Headers []string

	ControlPlaneName string

	TLSConfig TLSConfig
}

type KonnectRawState

type KonnectRawState struct {
	ServicePackages []*konnect.ServicePackage
	Documents       []*konnect.Document
}

KonnectRawState contains all of Konnect resources.

type TLSConfig added in v1.5.0

type TLSConfig struct {
	ServerName string
	CACert     string
	ClientCert string
	ClientKey  string
	SkipVerify bool
}

Jump to

Keyboard shortcuts

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