mirror of
https://github.com/open-cluster-management-io/ocm.git
synced 2026-02-14 10:00:11 +00:00
🌱 Move addon manager from addon-framework to ocm repo (#196)
* update vendor to add addon-framework Signed-off-by: zhujian <jiazhu@redhat.com> * Move addon manager from addon-framework to ocm repo Signed-off-by: zhujian <jiazhu@redhat.com> * add integration tests for addon manager Signed-off-by: zhujian <jiazhu@redhat.com> * push addon manager image post commit Signed-off-by: zhujian <jiazhu@redhat.com> * use library-go to refactor addon controllers Signed-off-by: zhujian <jiazhu@redhat.com> --------- Signed-off-by: zhujian <jiazhu@redhat.com>
This commit is contained in:
1
.github/workflows/e2e.yml
vendored
1
.github/workflows/e2e.yml
vendored
@@ -40,6 +40,7 @@ jobs:
|
||||
kind load docker-image --name=kind quay.io/open-cluster-management/registration:e2e
|
||||
kind load docker-image --name=kind quay.io/open-cluster-management/work:e2e
|
||||
kind load docker-image --name=kind quay.io/open-cluster-management/placement:e2e
|
||||
kind load docker-image --name=kind quay.io/open-cluster-management/addon-manager:e2e
|
||||
- name: Test E2E
|
||||
run: |
|
||||
IMAGE_TAG=e2e make test-e2e
|
||||
|
||||
11
.github/workflows/post.yml
vendored
11
.github/workflows/post.yml
vendored
@@ -68,6 +68,7 @@ jobs:
|
||||
docker push quay.io/open-cluster-management/registration:latest-${{ matrix.arch }}
|
||||
docker push quay.io/open-cluster-management/work:latest-${{ matrix.arch }}
|
||||
docker push quay.io/open-cluster-management/placement:latest-${{ matrix.arch }}
|
||||
docker push quay.io/open-cluster-management/addon-manager:latest-${{ matrix.arch }}
|
||||
image-manifest:
|
||||
name: image manifest
|
||||
runs-on: ubuntu-latest
|
||||
@@ -94,6 +95,10 @@ jobs:
|
||||
docker manifest create quay.io/open-cluster-management/placement:latest \
|
||||
quay.io/open-cluster-management/placement:latest-amd64 \
|
||||
quay.io/open-cluster-management/placement:latest-arm64
|
||||
# addon-manager
|
||||
docker manifest create quay.io/open-cluster-management/addon-manager:latest \
|
||||
quay.io/open-cluster-management/addon-manager:latest-amd64 \
|
||||
quay.io/open-cluster-management/addon-manager:latest-arm64
|
||||
- name: annotate
|
||||
run: |
|
||||
# registration-operator
|
||||
@@ -116,9 +121,15 @@ jobs:
|
||||
quay.io/open-cluster-management/placement:latest-amd64 --arch amd64
|
||||
docker manifest annotate quay.io/open-cluster-management/placement:latest \
|
||||
quay.io/open-cluster-management/placement:latest-arm64 --arch arm64
|
||||
# addon-manager
|
||||
docker manifest annotate quay.io/open-cluster-management/addon-manager:latest \
|
||||
quay.io/open-cluster-management/addon-manager:latest-amd64 --arch amd64
|
||||
docker manifest annotate quay.io/open-cluster-management/addon-manager:latest \
|
||||
quay.io/open-cluster-management/addon-manager:latest-arm64 --arch arm64
|
||||
- name: push
|
||||
run: |
|
||||
docker manifest push quay.io/open-cluster-management/registration-operator:latest
|
||||
docker manifest push quay.io/open-cluster-management/registration:latest
|
||||
docker manifest push quay.io/open-cluster-management/work:latest
|
||||
docker manifest push quay.io/open-cluster-management/placement:latest
|
||||
docker manifest push quay.io/open-cluster-management/addon-manager:latest
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,6 +3,7 @@
|
||||
/registration
|
||||
/work
|
||||
/registration-operator
|
||||
/addon
|
||||
|
||||
*.exe
|
||||
*.dll
|
||||
|
||||
3
Makefile
3
Makefile
@@ -40,12 +40,13 @@ REGISTRATION_IMAGE ?= $(IMAGE_REGISTRY)/registration:$(IMAGE_TAG)
|
||||
# PLACEMENT_IMAGE can be set in the env to override calculated value
|
||||
PLACEMENT_IMAGE ?= $(IMAGE_REGISTRY)/placement:$(IMAGE_TAG)
|
||||
# ADDON_MANAGER_IMAGE can be set in the env to override calculated value
|
||||
ADDON_MANAGER_IMAGE ?= $(IMAGE_REGISTRY)/addon-manager:latest
|
||||
ADDON_MANAGER_IMAGE ?= $(IMAGE_REGISTRY)/addon-manager:$(IMAGE_TAG)
|
||||
|
||||
$(call build-image,registration,$(REGISTRATION_IMAGE),./build/Dockerfile.registration,.)
|
||||
$(call build-image,work,$(WORK_IMAGE),./build/Dockerfile.work,.)
|
||||
$(call build-image,placement,$(PLACEMENT_IMAGE),./build/Dockerfile.placement,.)
|
||||
$(call build-image,registration-operator,$(OPERATOR_IMAGE_NAME),./build/Dockerfile.registration-operator,.)
|
||||
$(call build-image,addon-manager,$(ADDON_MANAGER_IMAGE),./build/Dockerfile.addon,.)
|
||||
|
||||
copy-crd:
|
||||
bash -x hack/copy-crds.sh
|
||||
|
||||
17
build/Dockerfile.addon
Normal file
17
build/Dockerfile.addon
Normal file
@@ -0,0 +1,17 @@
|
||||
FROM golang:1.20-bullseye AS builder
|
||||
ARG OS=linux
|
||||
ARG ARCH=amd64
|
||||
WORKDIR /go/src/open-cluster-management.io/ocm
|
||||
COPY . .
|
||||
ENV GO_PACKAGE open-cluster-management.io/ocm
|
||||
|
||||
RUN GOOS=${OS} \
|
||||
GOARCH=${ARCH} \
|
||||
GO_BUILD_PACKAGES=./cmd/addon \
|
||||
make build --warn-undefined-variables
|
||||
|
||||
FROM registry.access.redhat.com/ubi8/ubi-minimal:latest
|
||||
ENV USER_UID=10001
|
||||
COPY --from=builder /go/src/open-cluster-management.io/ocm/addon /
|
||||
|
||||
USER ${USER_UID}
|
||||
54
cmd/addon/main.go
Normal file
54
cmd/addon/main.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
goflag "flag"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
utilflag "k8s.io/component-base/cli/flag"
|
||||
"k8s.io/component-base/logs"
|
||||
|
||||
"open-cluster-management.io/ocm/pkg/cmd/hub"
|
||||
"open-cluster-management.io/ocm/pkg/version"
|
||||
)
|
||||
|
||||
func main() {
|
||||
rand.Seed(time.Now().UTC().UnixNano())
|
||||
|
||||
pflag.CommandLine.SetNormalizeFunc(utilflag.WordSepNormalizeFunc)
|
||||
pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
|
||||
|
||||
logs.AddFlags(pflag.CommandLine)
|
||||
logs.InitLogs()
|
||||
defer logs.FlushLogs()
|
||||
|
||||
command := newAddonCommand()
|
||||
if err := command.Execute(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func newAddonCommand() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "addon",
|
||||
Short: "Manager of Addon",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
_ = cmd.Help()
|
||||
os.Exit(1)
|
||||
},
|
||||
}
|
||||
|
||||
if v := version.Get().String(); len(v) == 0 {
|
||||
cmd.Version = "<unknown>"
|
||||
} else {
|
||||
cmd.Version = v
|
||||
}
|
||||
|
||||
cmd.AddCommand(hub.NewAddonManager())
|
||||
return cmd
|
||||
}
|
||||
25
go.mod
25
go.mod
@@ -4,7 +4,7 @@ go 1.20
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1
|
||||
github.com/evanphx/json-patch v4.12.0+incompatible
|
||||
github.com/evanphx/json-patch v5.6.0+incompatible
|
||||
github.com/google/go-cmp v0.5.9
|
||||
github.com/onsi/ginkgo/v2 v2.9.5
|
||||
github.com/onsi/gomega v1.27.7
|
||||
@@ -15,6 +15,7 @@ require (
|
||||
github.com/spf13/cobra v1.6.1
|
||||
github.com/spf13/pflag v1.0.5
|
||||
github.com/stretchr/testify v1.8.2
|
||||
github.com/valyala/fasttemplate v1.2.2
|
||||
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
|
||||
golang.org/x/net v0.10.0
|
||||
k8s.io/api v0.27.2
|
||||
@@ -26,6 +27,7 @@ require (
|
||||
k8s.io/klog/v2 v2.90.1
|
||||
k8s.io/kube-aggregator v0.27.2
|
||||
k8s.io/utils v0.0.0-20230313181309-38a27ef9d749
|
||||
open-cluster-management.io/addon-framework v0.7.1-0.20230626092851-963716af4eed
|
||||
open-cluster-management.io/api v0.11.1-0.20230609103311-088e8fe86139
|
||||
sigs.k8s.io/controller-runtime v0.15.0
|
||||
sigs.k8s.io/kube-storage-version-migrator v0.0.5
|
||||
@@ -33,17 +35,23 @@ require (
|
||||
|
||||
require (
|
||||
cloud.google.com/go/compute/metadata v0.2.3 // indirect
|
||||
github.com/BurntSushi/toml v1.2.1 // indirect
|
||||
github.com/Masterminds/goutils v1.1.1 // indirect
|
||||
github.com/Masterminds/semver/v3 v3.2.0 // indirect
|
||||
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
|
||||
github.com/NYTimes/gziphandler v1.1.1 // indirect
|
||||
github.com/antlr/antlr4/runtime/Go/antlr v1.4.10 // indirect
|
||||
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a // indirect
|
||||
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 // indirect
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/blang/semver/v4 v4.0.0 // indirect
|
||||
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||
github.com/coreos/go-semver v0.3.0 // indirect
|
||||
github.com/coreos/go-systemd/v22 v22.4.0 // indirect
|
||||
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
|
||||
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
|
||||
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
|
||||
github.com/fatih/structs v1.1.0 // indirect
|
||||
github.com/felixge/httpsnoop v1.0.3 // indirect
|
||||
github.com/fsnotify/fsnotify v1.6.0 // indirect
|
||||
github.com/ghodss/yaml v1.0.0 // indirect
|
||||
@@ -54,6 +62,7 @@ require (
|
||||
github.com/go-openapi/jsonreference v0.20.1 // indirect
|
||||
github.com/go-openapi/swag v0.22.3 // indirect
|
||||
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
|
||||
github.com/gobwas/glob v0.2.3 // indirect
|
||||
github.com/gogo/protobuf v1.3.2 // indirect
|
||||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
||||
github.com/golang/protobuf v1.5.3 // indirect
|
||||
@@ -64,13 +73,16 @@ require (
|
||||
github.com/google/uuid v1.3.0 // indirect
|
||||
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
|
||||
github.com/huandu/xstrings v1.3.3 // indirect
|
||||
github.com/imdario/mergo v0.3.12 // indirect
|
||||
github.com/inconshreveable/mousetrap v1.0.1 // indirect
|
||||
github.com/josharian/intern v1.0.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/mailru/easyjson v0.7.7 // indirect
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
|
||||
github.com/mitchellh/copystructure v1.2.0 // indirect
|
||||
github.com/mitchellh/mapstructure v1.4.1 // indirect
|
||||
github.com/mitchellh/reflectwalk v1.0.2 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||
@@ -82,8 +94,14 @@ require (
|
||||
github.com/prometheus/common v0.42.0 // indirect
|
||||
github.com/prometheus/procfs v0.9.0 // indirect
|
||||
github.com/robfig/cron v1.2.0 // indirect
|
||||
github.com/shopspring/decimal v1.2.0 // indirect
|
||||
github.com/sirupsen/logrus v1.9.0 // indirect
|
||||
github.com/spf13/cast v1.4.1 // indirect
|
||||
github.com/stoewer/go-strcase v1.2.0 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
|
||||
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
|
||||
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
|
||||
go.etcd.io/etcd/api/v3 v3.5.7 // indirect
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.5.7 // indirect
|
||||
go.etcd.io/etcd/client/v3 v3.5.7 // indirect
|
||||
@@ -100,7 +118,7 @@ require (
|
||||
go.uber.org/atomic v1.7.0 // indirect
|
||||
go.uber.org/multierr v1.6.0 // indirect
|
||||
go.uber.org/zap v1.24.0 // indirect
|
||||
golang.org/x/crypto v0.1.0 // indirect
|
||||
golang.org/x/crypto v0.5.0 // indirect
|
||||
golang.org/x/oauth2 v0.5.0 // indirect
|
||||
golang.org/x/sync v0.2.0 // indirect
|
||||
golang.org/x/sys v0.8.0 // indirect
|
||||
@@ -117,6 +135,7 @@ require (
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
helm.sh/helm/v3 v3.11.1 // indirect
|
||||
k8s.io/kms v0.27.2 // indirect
|
||||
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
|
||||
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.1.2 // indirect
|
||||
|
||||
74
go.sum
74
go.sum
@@ -51,9 +51,16 @@ github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6L
|
||||
github.com/Azure/go-autorest/logger v0.2.0/go.mod h1:T9E3cAhj2VqvPOtCYAvby9aBXkZmbF5NWuPV8+WeEW8=
|
||||
github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk=
|
||||
github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU=
|
||||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
|
||||
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
|
||||
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
|
||||
github.com/Masterminds/semver/v3 v3.2.0 h1:3MEsd0SM6jqZojhjLWWeBY+Kcjy9i6MQAeY7YgDP83g=
|
||||
github.com/Masterminds/semver/v3 v3.2.0/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
|
||||
github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA=
|
||||
github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM=
|
||||
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
|
||||
github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I=
|
||||
github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c=
|
||||
@@ -77,8 +84,9 @@ github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5
|
||||
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
|
||||
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
|
||||
github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
|
||||
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a h1:idn718Q4B6AGu/h5Sxe66HYVdqdGu2l9Iebqhi/AEoA=
|
||||
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
|
||||
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 h1:4daAzAu0S6Vi7/lbWECcX0j45yZReDZ56BQsrVBOEEY=
|
||||
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
|
||||
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
|
||||
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
|
||||
@@ -131,6 +139,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
|
||||
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/cyphar/filepath-securejoin v0.2.3 h1:YX6ebbZCZP7VkM3scTTokDgBL2TY741X51MTk3ycuNI=
|
||||
github.com/cyphar/filepath-securejoin v0.2.3/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
@@ -158,11 +168,13 @@ github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.
|
||||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||
github.com/evanphx/json-patch v4.2.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
|
||||
github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
|
||||
github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84=
|
||||
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
|
||||
github.com/evanphx/json-patch v5.6.0+incompatible h1:jBYDEEiFBPxA0v50tFdvOzQQTCvpL6mnFh5mB2/l16U=
|
||||
github.com/evanphx/json-patch v5.6.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
|
||||
github.com/evanphx/json-patch/v5 v5.6.0 h1:b91NhWfaz02IuVxO9faSllyAtNXHMPkC5J8sJCLunww=
|
||||
github.com/evanphx/json-patch/v5 v5.6.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2VvlbKOFpnXhI9Bw4=
|
||||
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
|
||||
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
|
||||
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
|
||||
github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
|
||||
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
|
||||
github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
|
||||
@@ -246,6 +258,8 @@ github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/me
|
||||
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
|
||||
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
|
||||
github.com/gobuffalo/flect v0.2.0/go.mod h1:W3K3X9ksuZfir8f/LrfVtWmCDQFfayuylOJ7sz/Fj80=
|
||||
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
|
||||
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
|
||||
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
|
||||
@@ -373,10 +387,13 @@ github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0m
|
||||
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
|
||||
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
|
||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||
github.com/huandu/xstrings v1.3.3 h1:/Gcsuc1x8JVbJ9/rlye4xZnVAbEkGauT8lbebqcQws4=
|
||||
github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
|
||||
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
|
||||
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
|
||||
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
|
||||
github.com/imdario/mergo v0.3.7/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
|
||||
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
||||
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
|
||||
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||
@@ -434,6 +451,9 @@ github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zk
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
|
||||
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
|
||||
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
|
||||
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
|
||||
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
|
||||
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
|
||||
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
|
||||
@@ -443,6 +463,9 @@ github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:F
|
||||
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
|
||||
github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag=
|
||||
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
|
||||
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
|
||||
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
|
||||
github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c=
|
||||
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635/go.mod h1:FBS0z0QWA44HXygs7VXDUOGoN/1TV3RuWkLO04am3wc=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
@@ -458,11 +481,13 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8m
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
|
||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
||||
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
|
||||
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
|
||||
github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
|
||||
github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/ginkgo v1.11.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
|
||||
github.com/onsi/ginkgo/v2 v2.9.5 h1:+6Hr4uxzP4XIUyAkg61dWBw8lb/gc4/X5luuxN/EC+Q=
|
||||
github.com/onsi/ginkgo/v2 v2.9.5/go.mod h1:tvAoo1QUJwNEU2ITftXTpR7R1RbCzoZUOs3RonqW57k=
|
||||
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
|
||||
@@ -530,6 +555,8 @@ github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
|
||||
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
|
||||
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
|
||||
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
|
||||
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
|
||||
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
|
||||
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
||||
@@ -544,6 +571,9 @@ github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasO
|
||||
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
|
||||
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
|
||||
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
|
||||
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
|
||||
github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA=
|
||||
github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
|
||||
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
|
||||
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
|
||||
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
|
||||
@@ -585,7 +615,17 @@ github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75 h1:6fotK7
|
||||
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
|
||||
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
|
||||
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
|
||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
|
||||
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
|
||||
github.com/vektah/gqlparser v1.1.2/go.mod h1:1ycwN7Ij5njmMkPPAOaRFY4rET2Enx7IkVv3vaXspKw=
|
||||
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
|
||||
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
|
||||
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
|
||||
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
|
||||
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
|
||||
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
|
||||
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 h1:eY9dn8+vbi4tKz5Qo6v2eYzo7kUS51QINcR5jNpbZS8=
|
||||
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
|
||||
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
|
||||
@@ -594,6 +634,7 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
|
||||
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
|
||||
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
|
||||
go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
|
||||
@@ -667,8 +708,10 @@ golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975/go.mod h1:LzIPMQfyMNhhGPh
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
|
||||
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
|
||||
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
|
||||
golang.org/x/crypto v0.5.0 h1:U/0M97KRkSFvyD/3FSmdP5W5swImpNgle/EHFhOsQPE=
|
||||
golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
|
||||
@@ -703,6 +746,7 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.3.1-0.20200828183125-ce943fd02449/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
|
||||
golang.org/x/net v0.0.0-20170114055629-f2499483f923/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
@@ -745,7 +789,10 @@ golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81R
|
||||
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.0.0-20210224082022-3d97a244fca7/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
|
||||
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
|
||||
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
@@ -766,6 +813,7 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ
|
||||
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI=
|
||||
golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20170830134202-bb24a47a89ea/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
@@ -818,13 +866,19 @@ golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
|
||||
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
|
||||
golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols=
|
||||
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
|
||||
golang.org/x/text v0.0.0-20160726164857-2910a502d2bf/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
@@ -835,6 +889,8 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
|
||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
@@ -900,6 +956,7 @@ golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc
|
||||
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
|
||||
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.9.1 h1:8WMNJAz3zrtPmnYC7ISf5dEn3MT0gY7jBJfw27yrrLo=
|
||||
golang.org/x/tools v0.9.1/go.mod h1:owI94Op576fPu3cIGQeHs3joujW/2Oc6MtlxbF5dfNc=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
@@ -1019,6 +1076,7 @@ gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXL
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
|
||||
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
|
||||
gopkg.in/square/go-jose.v2 v2.2.2/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
@@ -1038,6 +1096,8 @@ gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
|
||||
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
|
||||
gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8=
|
||||
helm.sh/helm/v3 v3.11.1 h1:cmL9fFohOoNQf+wnp2Wa0OhNFH0KFnSzEkVxi3fcc3I=
|
||||
helm.sh/helm/v3 v3.11.1/go.mod h1:z/Bu/BylToGno/6dtNGuSmjRqxKq5gaH+FU0BPO+AQ8=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
@@ -1096,6 +1156,8 @@ k8s.io/utils v0.0.0-20200324210504-a9aa75ae1b89/go.mod h1:sZAwmy6armz5eXlNoLmJcl
|
||||
k8s.io/utils v0.0.0-20201110183641-67b214c5f920/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
|
||||
k8s.io/utils v0.0.0-20230313181309-38a27ef9d749 h1:xMMXJlJbsU8w3V5N2FLDQ8YgU8s1EoULdbQBcAeNJkY=
|
||||
k8s.io/utils v0.0.0-20230313181309-38a27ef9d749/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
|
||||
open-cluster-management.io/addon-framework v0.7.1-0.20230626092851-963716af4eed h1:fOOKf8kzVNizc5fYvMwkPy9TT/vOpojd4IIxpzh/vhw=
|
||||
open-cluster-management.io/addon-framework v0.7.1-0.20230626092851-963716af4eed/go.mod h1:Cyt5knxR+sXaKvOfUKseZDAGulS2AJz6o7a9J0WXbak=
|
||||
open-cluster-management.io/api v0.11.1-0.20230609103311-088e8fe86139 h1:nw/XSv4eDGqmg0ks2PHzrE2uosvjw+D314843G56xGY=
|
||||
open-cluster-management.io/api v0.11.1-0.20230609103311-088e8fe86139/go.mod h1:WgKUCJ7+Bf40DsOmH1Gdkpyj3joco+QLzrlM6Ak39zE=
|
||||
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
|
||||
|
||||
@@ -43,7 +43,7 @@ spec:
|
||||
- name: addon-manager-controller
|
||||
image: {{ .AddOnManagerImage }}
|
||||
args:
|
||||
- "/addon-manager"
|
||||
- "/addon"
|
||||
- "manager"
|
||||
{{ if .HostedMode }}
|
||||
- "--kubeconfig=/var/run/secrets/hub/kubeconfig"
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
package addonconfiguration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
jsonpatch "github.com/evanphx/json-patch"
|
||||
"k8s.io/apimachinery/pkg/api/equality"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
||||
"k8s.io/klog/v2"
|
||||
|
||||
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
addonv1alpha1client "open-cluster-management.io/api/client/addon/clientset/versioned"
|
||||
)
|
||||
|
||||
type managedClusterAddonConfigurationReconciler struct {
|
||||
addonClient addonv1alpha1client.Interface
|
||||
}
|
||||
|
||||
func (d *managedClusterAddonConfigurationReconciler) reconcile(
|
||||
ctx context.Context, cma *addonv1alpha1.ClusterManagementAddOn, graph *configurationGraph) (*addonv1alpha1.ClusterManagementAddOn, reconcileState, error) {
|
||||
var errs []error
|
||||
|
||||
for _, addon := range graph.addonToUpdate() {
|
||||
mca := d.mergeAddonConfig(addon.mca, addon.desiredConfigs)
|
||||
err := d.patchAddonStatus(ctx, mca, addon.mca)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
|
||||
return cma, reconcileContinue, utilerrors.NewAggregate(errs)
|
||||
}
|
||||
|
||||
func (d *managedClusterAddonConfigurationReconciler) mergeAddonConfig(
|
||||
mca *addonv1alpha1.ManagedClusterAddOn, desiredConfigMap addonConfigMap) *addonv1alpha1.ManagedClusterAddOn {
|
||||
mcaCopy := mca.DeepCopy()
|
||||
|
||||
var mergedConfigs []addonv1alpha1.ConfigReference
|
||||
// remove configs that are not desired
|
||||
for _, config := range mcaCopy.Status.ConfigReferences {
|
||||
if _, ok := desiredConfigMap[config.ConfigGroupResource]; ok {
|
||||
mergedConfigs = append(mergedConfigs, config)
|
||||
}
|
||||
}
|
||||
|
||||
// append or update configs
|
||||
for _, config := range desiredConfigMap {
|
||||
var match bool
|
||||
for i := range mergedConfigs {
|
||||
if mergedConfigs[i].ConfigGroupResource != config.ConfigGroupResource {
|
||||
continue
|
||||
}
|
||||
|
||||
match = true
|
||||
// set LastObservedGeneration to 0 when config name/namespace changes
|
||||
if mergedConfigs[i].DesiredConfig != nil && (mergedConfigs[i].DesiredConfig.ConfigReferent != config.DesiredConfig.ConfigReferent) {
|
||||
mergedConfigs[i].LastObservedGeneration = 0
|
||||
}
|
||||
mergedConfigs[i].ConfigReferent = config.ConfigReferent
|
||||
mergedConfigs[i].DesiredConfig = config.DesiredConfig.DeepCopy()
|
||||
}
|
||||
|
||||
if !match {
|
||||
mergedConfigs = append(mergedConfigs, config)
|
||||
}
|
||||
}
|
||||
|
||||
mcaCopy.Status.ConfigReferences = mergedConfigs
|
||||
return mcaCopy
|
||||
}
|
||||
|
||||
func (d *managedClusterAddonConfigurationReconciler) patchAddonStatus(ctx context.Context, new, old *addonv1alpha1.ManagedClusterAddOn) error {
|
||||
if equality.Semantic.DeepEqual(new.Status, old.Status) {
|
||||
return nil
|
||||
}
|
||||
|
||||
oldData, err := json.Marshal(&addonv1alpha1.ManagedClusterAddOn{
|
||||
Status: addonv1alpha1.ManagedClusterAddOnStatus{
|
||||
Namespace: old.Status.Namespace,
|
||||
ConfigReferences: old.Status.ConfigReferences,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
newData, err := json.Marshal(&addonv1alpha1.ManagedClusterAddOn{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
UID: new.UID,
|
||||
ResourceVersion: new.ResourceVersion,
|
||||
},
|
||||
Status: addonv1alpha1.ManagedClusterAddOnStatus{
|
||||
Namespace: new.Status.Namespace,
|
||||
ConfigReferences: new.Status.ConfigReferences,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
patchBytes, err := jsonpatch.CreateMergePatch(oldData, newData)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create patch for addon %s: %w", new.Name, err)
|
||||
}
|
||||
|
||||
klog.V(2).Infof("Patching addon %s/%s status with %s", new.Namespace, new.Name, string(patchBytes))
|
||||
_, err = d.addonClient.AddonV1alpha1().ManagedClusterAddOns(new.Namespace).Patch(
|
||||
ctx, new.Name, types.MergePatchType, patchBytes, metav1.PatchOptions{}, "status")
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,630 @@
|
||||
package addonconfiguration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"sort"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
apiequality "k8s.io/apimachinery/pkg/api/equality"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
clienttesting "k8s.io/client-go/testing"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/addonmanager/addontesting"
|
||||
"open-cluster-management.io/addon-framework/pkg/index"
|
||||
"open-cluster-management.io/api/addon/v1alpha1"
|
||||
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
fakeaddon "open-cluster-management.io/api/client/addon/clientset/versioned/fake"
|
||||
addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions"
|
||||
fakecluster "open-cluster-management.io/api/client/cluster/clientset/versioned/fake"
|
||||
clusterv1informers "open-cluster-management.io/api/client/cluster/informers/externalversions"
|
||||
clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
|
||||
)
|
||||
|
||||
func TestAddonConfigReconcile(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
managedClusteraddon []runtime.Object
|
||||
clusterManagementAddon *addonv1alpha1.ClusterManagementAddOn
|
||||
placements []runtime.Object
|
||||
placementDecisions []runtime.Object
|
||||
validateAddonActions func(t *testing.T, actions []clienttesting.Action)
|
||||
expectErr bool
|
||||
}{
|
||||
{
|
||||
name: "no configuration",
|
||||
managedClusteraddon: []runtime.Object{
|
||||
addontesting.NewAddon("test", "cluster1"),
|
||||
addontesting.NewAddon("test", "cluster2"),
|
||||
},
|
||||
clusterManagementAddon: addontesting.NewClusterManagementAddon("test", "", "").Build(),
|
||||
placements: []runtime.Object{},
|
||||
placementDecisions: []runtime.Object{},
|
||||
validateAddonActions: addontesting.AssertNoActions,
|
||||
},
|
||||
{
|
||||
name: "manual installStrategy",
|
||||
managedClusteraddon: []runtime.Object{
|
||||
addontesting.NewAddon("test", "cluster1"),
|
||||
addontesting.NewAddon("test", "cluster2"),
|
||||
},
|
||||
clusterManagementAddon: addontesting.NewClusterManagementAddon("test", "", "").WithSupportedConfigs(addonv1alpha1.ConfigMeta{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DefaultConfig: &addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
}).WithDefaultConfigReferences(addonv1alpha1.DefaultConfigReference{
|
||||
ConfigGroupResource: v1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test"},
|
||||
SpecHash: "hash",
|
||||
},
|
||||
}).Build(),
|
||||
placements: []runtime.Object{},
|
||||
placementDecisions: []runtime.Object{},
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch", "patch")
|
||||
sort.Sort(byPatchName(actions))
|
||||
expectPatchConfigurationAction(t, actions[0], []addonv1alpha1.ConfigReference{{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
SpecHash: "hash",
|
||||
},
|
||||
LastObservedGeneration: 0,
|
||||
}})
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "placement installStrategy",
|
||||
managedClusteraddon: []runtime.Object{
|
||||
addontesting.NewAddon("test", "cluster1"),
|
||||
addontesting.NewAddon("test", "cluster2"),
|
||||
},
|
||||
placements: []runtime.Object{
|
||||
&clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: "test-placement", Namespace: "default"}},
|
||||
},
|
||||
placementDecisions: []runtime.Object{
|
||||
&clusterv1beta1.PlacementDecision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-placement",
|
||||
Namespace: "default",
|
||||
Labels: map[string]string{clusterv1beta1.PlacementLabel: "test-placement"},
|
||||
},
|
||||
Status: clusterv1beta1.PlacementDecisionStatus{
|
||||
Decisions: []clusterv1beta1.ClusterDecision{{ClusterName: "cluster2"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
clusterManagementAddon: addontesting.NewClusterManagementAddon("test", "", "").WithSupportedConfigs(addonv1alpha1.ConfigMeta{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DefaultConfig: &addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
}).WithDefaultConfigReferences(addonv1alpha1.DefaultConfigReference{
|
||||
ConfigGroupResource: v1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test"},
|
||||
SpecHash: "hash",
|
||||
},
|
||||
}).WithPlacementStrategy(addonv1alpha1.PlacementStrategy{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "test-placement", Namespace: "default"},
|
||||
}).WithInstallProgression(addonv1alpha1.InstallProgression{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "test-placement", Namespace: "default"},
|
||||
ConfigReferences: []addonv1alpha1.InstallConfigReference{
|
||||
{
|
||||
ConfigGroupResource: v1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
},
|
||||
},
|
||||
}).Build(),
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch", "patch")
|
||||
sort.Sort(byPatchName(actions))
|
||||
expectPatchConfigurationAction(t, actions[0], []addonv1alpha1.ConfigReference{{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
SpecHash: "hash",
|
||||
},
|
||||
LastObservedGeneration: 0,
|
||||
}})
|
||||
expectPatchConfigurationAction(t, actions[1], []addonv1alpha1.ConfigReference{{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
LastObservedGeneration: 0,
|
||||
}})
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "mca override",
|
||||
managedClusteraddon: []runtime.Object{
|
||||
newManagedClusterAddon("test", "cluster1", []addonv1alpha1.AddOnConfig{{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test2"},
|
||||
}}, nil),
|
||||
addontesting.NewAddon("test", "cluster2"),
|
||||
},
|
||||
placements: []runtime.Object{
|
||||
&clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: "test-placement", Namespace: "default"}},
|
||||
},
|
||||
placementDecisions: []runtime.Object{
|
||||
&clusterv1beta1.PlacementDecision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-placement",
|
||||
Namespace: "default",
|
||||
Labels: map[string]string{clusterv1beta1.PlacementLabel: "test-placement"},
|
||||
},
|
||||
Status: clusterv1beta1.PlacementDecisionStatus{
|
||||
Decisions: []clusterv1beta1.ClusterDecision{{ClusterName: "cluster1"}, {ClusterName: "cluster2"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
clusterManagementAddon: addontesting.NewClusterManagementAddon("test", "", "").WithSupportedConfigs(addonv1alpha1.ConfigMeta{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DefaultConfig: &addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
}).WithDefaultConfigReferences(addonv1alpha1.DefaultConfigReference{
|
||||
ConfigGroupResource: v1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test"},
|
||||
SpecHash: "hash",
|
||||
},
|
||||
}).WithPlacementStrategy(addonv1alpha1.PlacementStrategy{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "test-placement", Namespace: "default"},
|
||||
}).WithInstallProgression(addonv1alpha1.InstallProgression{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "test-placement", Namespace: "default"},
|
||||
ConfigReferences: []addonv1alpha1.InstallConfigReference{
|
||||
{
|
||||
ConfigGroupResource: v1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
},
|
||||
},
|
||||
}).Build(),
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch", "patch")
|
||||
sort.Sort(byPatchName(actions))
|
||||
expectPatchConfigurationAction(t, actions[0], []addonv1alpha1.ConfigReference{{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test2"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test2"},
|
||||
SpecHash: "",
|
||||
},
|
||||
LastObservedGeneration: 0,
|
||||
}})
|
||||
expectPatchConfigurationAction(t, actions[1], []addonv1alpha1.ConfigReference{{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
LastObservedGeneration: 0,
|
||||
}})
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "config name/namespce change",
|
||||
managedClusteraddon: []runtime.Object{
|
||||
newManagedClusterAddon("test", "cluster1", []addonv1alpha1.AddOnConfig{}, []addonv1alpha1.ConfigReference{{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
LastObservedGeneration: 1,
|
||||
}}),
|
||||
},
|
||||
placements: []runtime.Object{
|
||||
&clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: "test-placement", Namespace: "default"}},
|
||||
},
|
||||
placementDecisions: []runtime.Object{
|
||||
&clusterv1beta1.PlacementDecision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-placement",
|
||||
Namespace: "default",
|
||||
Labels: map[string]string{clusterv1beta1.PlacementLabel: "test-placement"},
|
||||
},
|
||||
Status: clusterv1beta1.PlacementDecisionStatus{
|
||||
Decisions: []clusterv1beta1.ClusterDecision{{ClusterName: "cluster1"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
clusterManagementAddon: addontesting.NewClusterManagementAddon("test", "", "").WithSupportedConfigs(addonv1alpha1.ConfigMeta{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DefaultConfig: &addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
}).WithPlacementStrategy(addonv1alpha1.PlacementStrategy{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "test-placement", Namespace: "default"},
|
||||
}).WithInstallProgression(addonv1alpha1.InstallProgression{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "test-placement", Namespace: "default"},
|
||||
ConfigReferences: []addonv1alpha1.InstallConfigReference{
|
||||
{
|
||||
ConfigGroupResource: v1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test2"},
|
||||
SpecHash: "hash2",
|
||||
},
|
||||
},
|
||||
},
|
||||
}).Build(),
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch")
|
||||
expectPatchConfigurationAction(t, actions[0], []addonv1alpha1.ConfigReference{{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test2"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test2"},
|
||||
SpecHash: "hash2",
|
||||
},
|
||||
LastObservedGeneration: 0,
|
||||
}})
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "config spec hash change",
|
||||
managedClusteraddon: []runtime.Object{
|
||||
newManagedClusterAddon("test", "cluster1", []addonv1alpha1.AddOnConfig{}, []addonv1alpha1.ConfigReference{{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
LastObservedGeneration: 1,
|
||||
}}),
|
||||
},
|
||||
placements: []runtime.Object{
|
||||
&clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: "test-placement", Namespace: "default"}},
|
||||
},
|
||||
placementDecisions: []runtime.Object{
|
||||
&clusterv1beta1.PlacementDecision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-placement",
|
||||
Namespace: "default",
|
||||
Labels: map[string]string{clusterv1beta1.PlacementLabel: "test-placement"},
|
||||
},
|
||||
Status: clusterv1beta1.PlacementDecisionStatus{
|
||||
Decisions: []clusterv1beta1.ClusterDecision{{ClusterName: "cluster1"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
clusterManagementAddon: addontesting.NewClusterManagementAddon("test", "", "").WithSupportedConfigs(addonv1alpha1.ConfigMeta{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DefaultConfig: &addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
}).WithPlacementStrategy(addonv1alpha1.PlacementStrategy{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "test-placement", Namespace: "default"},
|
||||
Configs: []addonv1alpha1.AddOnConfig{v1alpha1.AddOnConfig{
|
||||
ConfigGroupResource: v1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test1"}}},
|
||||
}).WithInstallProgression(addonv1alpha1.InstallProgression{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "test-placement", Namespace: "default"},
|
||||
ConfigReferences: []addonv1alpha1.InstallConfigReference{
|
||||
{
|
||||
ConfigGroupResource: v1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1new",
|
||||
},
|
||||
},
|
||||
},
|
||||
}).Build(),
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch")
|
||||
expectPatchConfigurationAction(t, actions[0], []addonv1alpha1.ConfigReference{{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1new",
|
||||
},
|
||||
LastObservedGeneration: 1,
|
||||
}})
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "mca noop",
|
||||
managedClusteraddon: []runtime.Object{
|
||||
newManagedClusterAddon("test", "cluster1", []addonv1alpha1.AddOnConfig{}, []addonv1alpha1.ConfigReference{{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
LastObservedGeneration: 1,
|
||||
}}),
|
||||
},
|
||||
placements: []runtime.Object{
|
||||
&clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: "test-placement", Namespace: "default"}},
|
||||
},
|
||||
placementDecisions: []runtime.Object{
|
||||
&clusterv1beta1.PlacementDecision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-placement",
|
||||
Namespace: "default",
|
||||
Labels: map[string]string{clusterv1beta1.PlacementLabel: "test-placement"},
|
||||
},
|
||||
Status: clusterv1beta1.PlacementDecisionStatus{
|
||||
Decisions: []clusterv1beta1.ClusterDecision{{ClusterName: "cluster1"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
clusterManagementAddon: addontesting.NewClusterManagementAddon("test", "", "").WithSupportedConfigs(addonv1alpha1.ConfigMeta{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DefaultConfig: &addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
}).WithPlacementStrategy(addonv1alpha1.PlacementStrategy{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "test-placement", Namespace: "default"},
|
||||
Configs: []addonv1alpha1.AddOnConfig{v1alpha1.AddOnConfig{
|
||||
ConfigGroupResource: v1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test1"}}},
|
||||
}).WithInstallProgression(addonv1alpha1.InstallProgression{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "test-placement", Namespace: "default"},
|
||||
ConfigReferences: []addonv1alpha1.InstallConfigReference{
|
||||
{
|
||||
ConfigGroupResource: v1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
},
|
||||
},
|
||||
}).Build(),
|
||||
validateAddonActions: addontesting.AssertNoActions,
|
||||
},
|
||||
{
|
||||
name: "placement rolling update with MaxConcurrency 1",
|
||||
managedClusteraddon: []runtime.Object{
|
||||
addontesting.NewAddon("test", "cluster1"),
|
||||
addontesting.NewAddon("test", "cluster2"),
|
||||
addontesting.NewAddon("test", "cluster3"),
|
||||
},
|
||||
placements: []runtime.Object{
|
||||
&clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: "test-placement", Namespace: "default"}},
|
||||
},
|
||||
placementDecisions: []runtime.Object{
|
||||
&clusterv1beta1.PlacementDecision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-placement",
|
||||
Namespace: "default",
|
||||
Labels: map[string]string{clusterv1beta1.PlacementLabel: "test-placement"},
|
||||
},
|
||||
Status: clusterv1beta1.PlacementDecisionStatus{
|
||||
Decisions: []clusterv1beta1.ClusterDecision{{ClusterName: "cluster2"}, {ClusterName: "cluster3"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
clusterManagementAddon: addontesting.NewClusterManagementAddon("test", "", "").WithPlacementStrategy(addonv1alpha1.PlacementStrategy{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "test-placement", Namespace: "default"},
|
||||
RolloutStrategy: addonv1alpha1.RolloutStrategy{
|
||||
Type: addonv1alpha1.AddonRolloutStrategyRollingUpdate,
|
||||
RollingUpdate: &addonv1alpha1.RollingUpdate{MaxConcurrency: intstr.FromInt(1)}},
|
||||
}).WithInstallProgression(addonv1alpha1.InstallProgression{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "test-placement", Namespace: "default"},
|
||||
ConfigReferences: []addonv1alpha1.InstallConfigReference{
|
||||
{
|
||||
ConfigGroupResource: v1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
},
|
||||
},
|
||||
}).Build(),
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch")
|
||||
sort.Sort(byPatchName(actions))
|
||||
expectPatchConfigurationAction(t, actions[0], []addonv1alpha1.ConfigReference{{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
LastObservedGeneration: 0,
|
||||
}})
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "placement rolling update with MaxConcurrency 0",
|
||||
managedClusteraddon: []runtime.Object{
|
||||
addontesting.NewAddon("test", "cluster1"),
|
||||
addontesting.NewAddon("test", "cluster2"),
|
||||
addontesting.NewAddon("test", "cluster3"),
|
||||
},
|
||||
placements: []runtime.Object{
|
||||
&clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: "test-placement", Namespace: "default"}},
|
||||
},
|
||||
placementDecisions: []runtime.Object{
|
||||
&clusterv1beta1.PlacementDecision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-placement",
|
||||
Namespace: "default",
|
||||
Labels: map[string]string{clusterv1beta1.PlacementLabel: "test-placement"},
|
||||
},
|
||||
Status: clusterv1beta1.PlacementDecisionStatus{
|
||||
Decisions: []clusterv1beta1.ClusterDecision{{ClusterName: "cluster2"}, {ClusterName: "cluster3"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
clusterManagementAddon: addontesting.NewClusterManagementAddon("test", "", "").WithPlacementStrategy(addonv1alpha1.PlacementStrategy{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "test-placement", Namespace: "default"},
|
||||
RolloutStrategy: addonv1alpha1.RolloutStrategy{
|
||||
Type: addonv1alpha1.AddonRolloutStrategyRollingUpdate,
|
||||
RollingUpdate: &addonv1alpha1.RollingUpdate{MaxConcurrency: intstr.FromString("0%")}},
|
||||
}).WithInstallProgression(addonv1alpha1.InstallProgression{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "test-placement", Namespace: "default"},
|
||||
ConfigReferences: []addonv1alpha1.InstallConfigReference{
|
||||
{
|
||||
ConfigGroupResource: v1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
},
|
||||
},
|
||||
}).Build(),
|
||||
validateAddonActions: addontesting.AssertNoActions,
|
||||
},
|
||||
{
|
||||
name: "placement rolling update with default MaxConcurrency",
|
||||
managedClusteraddon: []runtime.Object{
|
||||
addontesting.NewAddon("test", "cluster1"),
|
||||
addontesting.NewAddon("test", "cluster2"),
|
||||
addontesting.NewAddon("test", "cluster3"),
|
||||
},
|
||||
placements: []runtime.Object{
|
||||
&clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: "test-placement", Namespace: "default"}},
|
||||
},
|
||||
placementDecisions: []runtime.Object{
|
||||
&clusterv1beta1.PlacementDecision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-placement",
|
||||
Namespace: "default",
|
||||
Labels: map[string]string{clusterv1beta1.PlacementLabel: "test-placement"},
|
||||
},
|
||||
Status: clusterv1beta1.PlacementDecisionStatus{
|
||||
Decisions: []clusterv1beta1.ClusterDecision{{ClusterName: "cluster2"}, {ClusterName: "cluster3"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
clusterManagementAddon: addontesting.NewClusterManagementAddon("test", "", "").WithPlacementStrategy(addonv1alpha1.PlacementStrategy{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "test-placement", Namespace: "default"},
|
||||
RolloutStrategy: addonv1alpha1.RolloutStrategy{
|
||||
Type: addonv1alpha1.AddonRolloutStrategyRollingUpdate,
|
||||
RollingUpdate: &addonv1alpha1.RollingUpdate{MaxConcurrency: defaultMaxConcurrency}},
|
||||
}).WithInstallProgression(addonv1alpha1.InstallProgression{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "test-placement", Namespace: "default"},
|
||||
ConfigReferences: []addonv1alpha1.InstallConfigReference{
|
||||
{
|
||||
ConfigGroupResource: v1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
},
|
||||
},
|
||||
}).Build(),
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch")
|
||||
sort.Sort(byPatchName(actions))
|
||||
expectPatchConfigurationAction(t, actions[0], []addonv1alpha1.ConfigReference{{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
LastObservedGeneration: 0,
|
||||
}})
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
clusterObj := append(c.placements, c.placementDecisions...)
|
||||
fakeClusterClient := fakecluster.NewSimpleClientset(clusterObj...)
|
||||
fakeAddonClient := fakeaddon.NewSimpleClientset(c.managedClusteraddon...)
|
||||
|
||||
addonInformers := addoninformers.NewSharedInformerFactory(fakeAddonClient, 10*time.Minute)
|
||||
clusterInformers := clusterv1informers.NewSharedInformerFactory(fakeClusterClient, 10*time.Minute)
|
||||
|
||||
err := addonInformers.Addon().V1alpha1().ManagedClusterAddOns().Informer().AddIndexers(
|
||||
cache.Indexers{
|
||||
index.ManagedClusterAddonByName: index.IndexManagedClusterAddonByName,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, obj := range c.placements {
|
||||
if err := clusterInformers.Cluster().V1beta1().Placements().Informer().GetStore().Add(obj); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, obj := range c.placementDecisions {
|
||||
if err := clusterInformers.Cluster().V1beta1().PlacementDecisions().Informer().GetStore().Add(obj); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, obj := range c.managedClusteraddon {
|
||||
if err := addonInformers.Addon().V1alpha1().ManagedClusterAddOns().Informer().GetStore().Add(obj); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
controller := &addonConfigurationController{
|
||||
addonClient: fakeAddonClient,
|
||||
placementDecisionLister: clusterInformers.Cluster().V1beta1().PlacementDecisions().Lister(),
|
||||
placementLister: clusterInformers.Cluster().V1beta1().Placements().Lister(),
|
||||
clusterManagementAddonLister: addonInformers.Addon().V1alpha1().ClusterManagementAddOns().Lister(),
|
||||
managedClusterAddonIndexer: addonInformers.Addon().V1alpha1().ManagedClusterAddOns().Informer().GetIndexer(),
|
||||
}
|
||||
|
||||
reconcile := &managedClusterAddonConfigurationReconciler{
|
||||
addonClient: fakeAddonClient,
|
||||
}
|
||||
|
||||
graph, err := controller.buildConfigurationGraph(c.clusterManagementAddon)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error when build graph: %v", err)
|
||||
}
|
||||
|
||||
_, _, err = reconcile.reconcile(context.TODO(), c.clusterManagementAddon, graph)
|
||||
if err != nil && !c.expectErr {
|
||||
t.Errorf("expected no error when sync: %v", err)
|
||||
}
|
||||
if err == nil && c.expectErr {
|
||||
t.Errorf("Expect error but got no error")
|
||||
}
|
||||
|
||||
c.validateAddonActions(t, fakeAddonClient.Actions())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// the Age field.
|
||||
type byPatchName []clienttesting.Action
|
||||
|
||||
func (a byPatchName) Len() int { return len(a) }
|
||||
func (a byPatchName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a byPatchName) Less(i, j int) bool {
|
||||
patchi := a[i].(clienttesting.PatchActionImpl)
|
||||
patchj := a[j].(clienttesting.PatchActionImpl)
|
||||
return patchi.Namespace < patchj.Namespace
|
||||
}
|
||||
|
||||
func newManagedClusterAddon(name, namespace string, configs []addonv1alpha1.AddOnConfig, configStatus []addonv1alpha1.ConfigReference) *addonv1alpha1.ManagedClusterAddOn {
|
||||
mca := addontesting.NewAddon(name, namespace)
|
||||
mca.Spec.Configs = configs
|
||||
mca.Status.ConfigReferences = configStatus
|
||||
return mca
|
||||
}
|
||||
|
||||
func expectPatchConfigurationAction(t *testing.T, action clienttesting.Action, expected []addonv1alpha1.ConfigReference) {
|
||||
patch := action.(clienttesting.PatchActionImpl).GetPatch()
|
||||
mca := &addonv1alpha1.ManagedClusterAddOn{}
|
||||
err := json.Unmarshal(patch, mca)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !apiequality.Semantic.DeepEqual(mca.Status.ConfigReferences, expected) {
|
||||
t.Errorf("Configuration not correctly patched, expected %v, actual %v", expected, mca.Status.ConfigReferences)
|
||||
}
|
||||
}
|
||||
212
pkg/addon/controllers/addonconfiguration/controller.go
Normal file
212
pkg/addon/controllers/addonconfiguration/controller.go
Normal file
@@ -0,0 +1,212 @@
|
||||
package addonconfiguration
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/openshift/library-go/pkg/controller/factory"
|
||||
"github.com/openshift/library-go/pkg/operator/events"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
"k8s.io/klog/v2"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/index"
|
||||
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
addonv1alpha1client "open-cluster-management.io/api/client/addon/clientset/versioned"
|
||||
addoninformerv1alpha1 "open-cluster-management.io/api/client/addon/informers/externalversions/addon/v1alpha1"
|
||||
addonlisterv1alpha1 "open-cluster-management.io/api/client/addon/listers/addon/v1alpha1"
|
||||
clusterinformersv1beta1 "open-cluster-management.io/api/client/cluster/informers/externalversions/cluster/v1beta1"
|
||||
clusterlisterv1beta1 "open-cluster-management.io/api/client/cluster/listers/cluster/v1beta1"
|
||||
clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
|
||||
)
|
||||
|
||||
// addonConfigurationController is a controller to update configuration of mca with the following order
|
||||
// 1. use configuration in mca spec if it is set
|
||||
// 2. use configuration in install strategy
|
||||
// 3. use configuration in the default configuration in cma
|
||||
type addonConfigurationController struct {
|
||||
addonClient addonv1alpha1client.Interface
|
||||
clusterManagementAddonLister addonlisterv1alpha1.ClusterManagementAddOnLister
|
||||
managedClusterAddonIndexer cache.Indexer
|
||||
addonFilterFunc factory.EventFilterFunc
|
||||
placementLister clusterlisterv1beta1.PlacementLister
|
||||
placementDecisionLister clusterlisterv1beta1.PlacementDecisionLister
|
||||
|
||||
reconcilers []addonConfigurationReconcile
|
||||
}
|
||||
|
||||
type addonConfigurationReconcile interface {
|
||||
reconcile(ctx context.Context, cma *addonv1alpha1.ClusterManagementAddOn,
|
||||
graph *configurationGraph) (*addonv1alpha1.ClusterManagementAddOn, reconcileState, error)
|
||||
}
|
||||
|
||||
type reconcileState int64
|
||||
|
||||
const (
|
||||
reconcileStop reconcileState = iota
|
||||
reconcileContinue
|
||||
)
|
||||
|
||||
func NewAddonConfigurationController(
|
||||
addonClient addonv1alpha1client.Interface,
|
||||
addonInformers addoninformerv1alpha1.ManagedClusterAddOnInformer,
|
||||
clusterManagementAddonInformers addoninformerv1alpha1.ClusterManagementAddOnInformer,
|
||||
placementInformer clusterinformersv1beta1.PlacementInformer,
|
||||
placementDecisionInformer clusterinformersv1beta1.PlacementDecisionInformer,
|
||||
addonFilterFunc factory.EventFilterFunc,
|
||||
recorder events.Recorder,
|
||||
) factory.Controller {
|
||||
c := &addonConfigurationController{
|
||||
addonClient: addonClient,
|
||||
clusterManagementAddonLister: clusterManagementAddonInformers.Lister(),
|
||||
managedClusterAddonIndexer: addonInformers.Informer().GetIndexer(),
|
||||
addonFilterFunc: addonFilterFunc,
|
||||
}
|
||||
|
||||
c.reconcilers = []addonConfigurationReconcile{
|
||||
&managedClusterAddonConfigurationReconciler{
|
||||
addonClient: addonClient,
|
||||
},
|
||||
&clusterManagementAddonProgressingReconciler{
|
||||
addonClient: addonClient,
|
||||
},
|
||||
}
|
||||
|
||||
controllerFactory := factory.New().WithFilteredEventsInformersQueueKeysFunc(
|
||||
func(obj runtime.Object) []string {
|
||||
key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
|
||||
return []string{key}
|
||||
},
|
||||
c.addonFilterFunc,
|
||||
clusterManagementAddonInformers.Informer()).WithInformersQueueKeysFunc(
|
||||
func(obj runtime.Object) []string {
|
||||
key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
|
||||
return []string{key}
|
||||
},
|
||||
addonInformers.Informer())
|
||||
|
||||
// This is to handle the case the self managed addon-manager does not have placementInformer/placementDecisionInformer.
|
||||
// we will not consider installStrategy related placement for self managed addon-manager.
|
||||
if placementInformer != nil && placementDecisionInformer != nil {
|
||||
controllerFactory = controllerFactory.WithInformersQueueKeysFunc(
|
||||
index.ClusterManagementAddonByPlacementDecisionQueueKey(clusterManagementAddonInformers), placementDecisionInformer.Informer()).
|
||||
WithInformersQueueKeysFunc(index.ClusterManagementAddonByPlacementQueueKey(clusterManagementAddonInformers), placementInformer.Informer())
|
||||
c.placementLister = placementInformer.Lister()
|
||||
c.placementDecisionLister = placementDecisionInformer.Lister()
|
||||
}
|
||||
|
||||
return controllerFactory.WithSync(c.sync).ToController("addon-configuration-controller", recorder)
|
||||
}
|
||||
|
||||
func (c *addonConfigurationController) sync(ctx context.Context, syncCtx factory.SyncContext) error {
|
||||
key := syncCtx.QueueKey()
|
||||
_, addonName, err := cache.SplitMetaNamespaceKey(key)
|
||||
if err != nil {
|
||||
// ignore addon whose key is invalid
|
||||
return nil
|
||||
}
|
||||
|
||||
klog.V(4).Infof("Reconciling addon %q", addonName)
|
||||
|
||||
cma, err := c.clusterManagementAddonLister.Get(addonName)
|
||||
switch {
|
||||
case errors.IsNotFound(err):
|
||||
return nil
|
||||
case err != nil:
|
||||
return err
|
||||
}
|
||||
|
||||
if !c.addonFilterFunc(cma) {
|
||||
return nil
|
||||
}
|
||||
|
||||
cma = cma.DeepCopy()
|
||||
graph, err := c.buildConfigurationGraph(cma)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var state reconcileState
|
||||
var errs []error
|
||||
for _, reconciler := range c.reconcilers {
|
||||
cma, state, err = reconciler.reconcile(ctx, cma, graph)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
if state == reconcileStop {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return utilerrors.NewAggregate(errs)
|
||||
}
|
||||
|
||||
func (c *addonConfigurationController) buildConfigurationGraph(cma *addonv1alpha1.ClusterManagementAddOn) (*configurationGraph, error) {
|
||||
graph := newGraph(cma.Spec.SupportedConfigs, cma.Status.DefaultConfigReferences)
|
||||
addons, err := c.managedClusterAddonIndexer.ByIndex(index.ManagedClusterAddonByName, cma.Name)
|
||||
if err != nil {
|
||||
return graph, err
|
||||
}
|
||||
|
||||
// add all existing addons to the default at first
|
||||
for _, addonObject := range addons {
|
||||
addon := addonObject.(*addonv1alpha1.ManagedClusterAddOn)
|
||||
graph.addAddonNode(addon)
|
||||
}
|
||||
|
||||
if cma.Spec.InstallStrategy.Type == "" || cma.Spec.InstallStrategy.Type == addonv1alpha1.AddonInstallStrategyManual {
|
||||
return graph, nil
|
||||
}
|
||||
|
||||
// check each install strategy in status
|
||||
var errs []error
|
||||
for _, installProgression := range cma.Status.InstallProgressions {
|
||||
clusters, err := c.getClustersByPlacement(installProgression.PlacementRef.Name, installProgression.PlacementRef.Namespace)
|
||||
if errors.IsNotFound(err) {
|
||||
klog.V(2).Infof("placement %s/%s is not found for addon %s", installProgression.PlacementRef.Namespace, installProgression.PlacementRef.Name, cma.Name)
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, installStrategy := range cma.Spec.InstallStrategy.Placements {
|
||||
if installStrategy.PlacementRef == installProgression.PlacementRef {
|
||||
graph.addPlacementNode(installStrategy, installProgression, clusters)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return graph, utilerrors.NewAggregate(errs)
|
||||
}
|
||||
|
||||
func (c *addonConfigurationController) getClustersByPlacement(name, namespace string) ([]string, error) {
|
||||
var clusters []string
|
||||
if c.placementLister == nil || c.placementDecisionLister == nil {
|
||||
return clusters, nil
|
||||
}
|
||||
_, err := c.placementLister.Placements(namespace).Get(name)
|
||||
if err != nil {
|
||||
return clusters, err
|
||||
}
|
||||
|
||||
decisionSelector := labels.SelectorFromSet(labels.Set{
|
||||
clusterv1beta1.PlacementLabel: name,
|
||||
})
|
||||
decisions, err := c.placementDecisionLister.PlacementDecisions(namespace).List(decisionSelector)
|
||||
if err != nil {
|
||||
return clusters, err
|
||||
}
|
||||
|
||||
for _, d := range decisions {
|
||||
for _, sd := range d.Status.Decisions {
|
||||
clusters = append(clusters, sd.ClusterName)
|
||||
}
|
||||
}
|
||||
|
||||
return clusters, nil
|
||||
}
|
||||
343
pkg/addon/controllers/addonconfiguration/graph.go
Normal file
343
pkg/addon/controllers/addonconfiguration/graph.go
Normal file
@@ -0,0 +1,343 @@
|
||||
package addonconfiguration
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/equality"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
|
||||
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultMaxConcurrency = intstr.FromString("25%")
|
||||
maxMaxConcurrency = intstr.FromString("100%")
|
||||
)
|
||||
|
||||
// configurationTree is a 2 level snapshot tree on the configuration of addons
|
||||
// the first level is a list of nodes that represents a install strategy and a desired configuration for this install
|
||||
// strategy. The second level is a list of nodes that represent each mca and its desired configuration
|
||||
type configurationGraph struct {
|
||||
// nodes maintains a list between a installStrategy and its related mcas
|
||||
nodes []*installStrategyNode
|
||||
// defaults is the nodes with no install strategy
|
||||
defaults *installStrategyNode
|
||||
}
|
||||
|
||||
// installStrategyNode is a node in configurationGraph defined by a install strategy
|
||||
type installStrategyNode struct {
|
||||
placementRef addonv1alpha1.PlacementRef
|
||||
maxConcurrency intstr.IntOrString
|
||||
desiredConfigs addonConfigMap
|
||||
// children keeps a map of addons node as the children of this node
|
||||
children map[string]*addonNode
|
||||
clusters sets.Set[string]
|
||||
}
|
||||
|
||||
// addonNode is node as a child of installStrategy node represting a mca
|
||||
// addonnode
|
||||
type addonNode struct {
|
||||
desiredConfigs addonConfigMap
|
||||
mca *addonv1alpha1.ManagedClusterAddOn
|
||||
// record mca upgrade status
|
||||
mcaUpgradeStatus upgradeStatus
|
||||
}
|
||||
|
||||
type upgradeStatus int
|
||||
|
||||
const (
|
||||
// mca desired configs not synced from desiredConfigs yet
|
||||
toupgrade upgradeStatus = iota
|
||||
// mca desired configs upgraded and last applied configs not upgraded
|
||||
upgrading
|
||||
// both desired configs and last applied configs are upgraded
|
||||
upgraded
|
||||
)
|
||||
|
||||
type addonConfigMap map[addonv1alpha1.ConfigGroupResource]addonv1alpha1.ConfigReference
|
||||
|
||||
// set addon upgrade status
|
||||
func (n *addonNode) setUpgradeStatus() {
|
||||
if len(n.mca.Status.ConfigReferences) != len(n.desiredConfigs) {
|
||||
n.mcaUpgradeStatus = toupgrade
|
||||
return
|
||||
}
|
||||
|
||||
for _, actual := range n.mca.Status.ConfigReferences {
|
||||
if desired, ok := n.desiredConfigs[actual.ConfigGroupResource]; ok {
|
||||
if !equality.Semantic.DeepEqual(desired.DesiredConfig, actual.DesiredConfig) {
|
||||
n.mcaUpgradeStatus = toupgrade
|
||||
return
|
||||
} else if !equality.Semantic.DeepEqual(actual.LastAppliedConfig, actual.DesiredConfig) {
|
||||
n.mcaUpgradeStatus = upgrading
|
||||
return
|
||||
}
|
||||
} else {
|
||||
n.mcaUpgradeStatus = toupgrade
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
n.mcaUpgradeStatus = upgraded
|
||||
}
|
||||
|
||||
func (d addonConfigMap) copy() addonConfigMap {
|
||||
output := addonConfigMap{}
|
||||
for k, v := range d {
|
||||
output[k] = v
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
func newGraph(supportedConfigs []addonv1alpha1.ConfigMeta, defaultConfigReferences []addonv1alpha1.DefaultConfigReference) *configurationGraph {
|
||||
graph := &configurationGraph{
|
||||
nodes: []*installStrategyNode{},
|
||||
defaults: &installStrategyNode{
|
||||
maxConcurrency: maxMaxConcurrency,
|
||||
desiredConfigs: map[addonv1alpha1.ConfigGroupResource]addonv1alpha1.ConfigReference{},
|
||||
children: map[string]*addonNode{},
|
||||
},
|
||||
}
|
||||
|
||||
// init graph.defaults.desiredConfigs with supportedConfigs
|
||||
for _, config := range supportedConfigs {
|
||||
if config.DefaultConfig != nil {
|
||||
graph.defaults.desiredConfigs[config.ConfigGroupResource] = addonv1alpha1.ConfigReference{
|
||||
ConfigGroupResource: config.ConfigGroupResource,
|
||||
ConfigReferent: *config.DefaultConfig,
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: *config.DefaultConfig,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
// copy the spechash from cma status defaultConfigReferences
|
||||
for _, configRef := range defaultConfigReferences {
|
||||
if configRef.DesiredConfig == nil {
|
||||
continue
|
||||
}
|
||||
defaultsDesiredConfig, ok := graph.defaults.desiredConfigs[configRef.ConfigGroupResource]
|
||||
if ok && (defaultsDesiredConfig.DesiredConfig.ConfigReferent == configRef.DesiredConfig.ConfigReferent) {
|
||||
defaultsDesiredConfig.DesiredConfig.SpecHash = configRef.DesiredConfig.SpecHash
|
||||
}
|
||||
}
|
||||
|
||||
return graph
|
||||
}
|
||||
|
||||
// addAddonNode to the graph, starting from placement with the highest order
|
||||
func (g *configurationGraph) addAddonNode(mca *addonv1alpha1.ManagedClusterAddOn) {
|
||||
for i := len(g.nodes) - 1; i >= 0; i-- {
|
||||
if g.nodes[i].clusters.Has(mca.Namespace) {
|
||||
g.nodes[i].addNode(mca)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
g.defaults.addNode(mca)
|
||||
}
|
||||
|
||||
// addNode delete clusters on existing graph so the new configuration overrides the previous
|
||||
func (g *configurationGraph) addPlacementNode(
|
||||
installStrategy addonv1alpha1.PlacementStrategy,
|
||||
installProgression addonv1alpha1.InstallProgression,
|
||||
clusters []string,
|
||||
) {
|
||||
placementRef := installProgression.PlacementRef
|
||||
installConfigReference := installProgression.ConfigReferences
|
||||
|
||||
node := &installStrategyNode{
|
||||
placementRef: placementRef,
|
||||
maxConcurrency: maxMaxConcurrency,
|
||||
desiredConfigs: g.defaults.desiredConfigs,
|
||||
children: map[string]*addonNode{},
|
||||
clusters: sets.New[string](clusters...),
|
||||
}
|
||||
|
||||
// set max concurrency
|
||||
if installStrategy.RolloutStrategy.Type == addonv1alpha1.AddonRolloutStrategyRollingUpdate {
|
||||
if installStrategy.RolloutStrategy.RollingUpdate != nil {
|
||||
node.maxConcurrency = installStrategy.RolloutStrategy.RollingUpdate.MaxConcurrency
|
||||
} else {
|
||||
node.maxConcurrency = defaultMaxConcurrency
|
||||
}
|
||||
}
|
||||
|
||||
// overrides configuration by install strategy
|
||||
if len(installConfigReference) > 0 {
|
||||
node.desiredConfigs = node.desiredConfigs.copy()
|
||||
for _, configRef := range installConfigReference {
|
||||
if configRef.DesiredConfig == nil {
|
||||
continue
|
||||
}
|
||||
node.desiredConfigs[configRef.ConfigGroupResource] = addonv1alpha1.ConfigReference{
|
||||
ConfigGroupResource: configRef.ConfigGroupResource,
|
||||
ConfigReferent: configRef.DesiredConfig.ConfigReferent,
|
||||
DesiredConfig: configRef.DesiredConfig.DeepCopy(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// remove addon in defaults and other placements.
|
||||
for _, cluster := range clusters {
|
||||
if _, ok := g.defaults.children[cluster]; ok {
|
||||
node.addNode(g.defaults.children[cluster].mca)
|
||||
delete(g.defaults.children, cluster)
|
||||
}
|
||||
for _, placement := range g.nodes {
|
||||
if _, ok := placement.children[cluster]; ok {
|
||||
node.addNode(placement.children[cluster].mca)
|
||||
delete(placement.children, cluster)
|
||||
}
|
||||
}
|
||||
}
|
||||
g.nodes = append(g.nodes, node)
|
||||
}
|
||||
|
||||
func (g *configurationGraph) getPlacementNodes() map[addonv1alpha1.PlacementRef]*installStrategyNode {
|
||||
placementNodeMap := map[addonv1alpha1.PlacementRef]*installStrategyNode{}
|
||||
for _, node := range g.nodes {
|
||||
placementNodeMap[node.placementRef] = node
|
||||
}
|
||||
|
||||
return placementNodeMap
|
||||
}
|
||||
|
||||
func (g *configurationGraph) addonToUpdate() []*addonNode {
|
||||
var addons []*addonNode
|
||||
for _, node := range g.nodes {
|
||||
addons = append(addons, node.addonToUpdate()...)
|
||||
}
|
||||
|
||||
addons = append(addons, g.defaults.addonToUpdate()...)
|
||||
|
||||
return addons
|
||||
}
|
||||
|
||||
func (n *installStrategyNode) addNode(addon *addonv1alpha1.ManagedClusterAddOn) {
|
||||
n.children[addon.Namespace] = &addonNode{
|
||||
mca: addon,
|
||||
desiredConfigs: n.desiredConfigs,
|
||||
}
|
||||
|
||||
// override configuration by mca spec
|
||||
if len(addon.Spec.Configs) > 0 {
|
||||
n.children[addon.Namespace].desiredConfigs = n.children[addon.Namespace].desiredConfigs.copy()
|
||||
// TODO we should also filter out the configs which are not supported configs.
|
||||
for _, config := range addon.Spec.Configs {
|
||||
n.children[addon.Namespace].desiredConfigs[config.ConfigGroupResource] = addonv1alpha1.ConfigReference{
|
||||
ConfigGroupResource: config.ConfigGroupResource,
|
||||
ConfigReferent: config.ConfigReferent,
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: config.ConfigReferent,
|
||||
},
|
||||
}
|
||||
// copy the spechash from mca status
|
||||
for _, configRef := range addon.Status.ConfigReferences {
|
||||
if configRef.DesiredConfig == nil {
|
||||
continue
|
||||
}
|
||||
nodeDesiredConfig, ok := n.children[addon.Namespace].desiredConfigs[configRef.ConfigGroupResource]
|
||||
if ok && (nodeDesiredConfig.DesiredConfig.ConfigReferent == configRef.DesiredConfig.ConfigReferent) {
|
||||
nodeDesiredConfig.DesiredConfig.SpecHash = configRef.DesiredConfig.SpecHash
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// set addon node upgrade status
|
||||
n.children[addon.Namespace].setUpgradeStatus()
|
||||
}
|
||||
|
||||
func (n *installStrategyNode) addonUpgraded() int {
|
||||
count := 0
|
||||
for _, addon := range n.children {
|
||||
if desiredConfigsEqual(addon.desiredConfigs, n.desiredConfigs) && addon.mcaUpgradeStatus == upgraded {
|
||||
count += 1
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func (n *installStrategyNode) addonUpgrading() int {
|
||||
count := 0
|
||||
for _, addon := range n.children {
|
||||
if desiredConfigsEqual(addon.desiredConfigs, n.desiredConfigs) && addon.mcaUpgradeStatus == upgrading {
|
||||
count += 1
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// addonToUpdate finds the addons to be updated by placement
|
||||
func (n *installStrategyNode) addonToUpdate() []*addonNode {
|
||||
var addons []*addonNode
|
||||
|
||||
// sort the children by key
|
||||
keys := make([]string, 0, len(n.children))
|
||||
for k := range n.children {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
|
||||
total := len(n.clusters)
|
||||
if total == 0 {
|
||||
total = len(n.children)
|
||||
}
|
||||
|
||||
length, _ := parseMaxConcurrency(n.maxConcurrency, total)
|
||||
if length == 0 {
|
||||
return addons
|
||||
}
|
||||
|
||||
for i, k := range keys {
|
||||
if (i%length == 0) && len(addons) > 0 {
|
||||
return addons
|
||||
}
|
||||
|
||||
addon := n.children[k]
|
||||
if addon.mcaUpgradeStatus != upgraded {
|
||||
addons = append(addons, addon)
|
||||
}
|
||||
}
|
||||
|
||||
return addons
|
||||
}
|
||||
|
||||
func parseMaxConcurrency(maxConcurrency intstr.IntOrString, total int) (int, error) {
|
||||
var length int
|
||||
|
||||
switch maxConcurrency.Type {
|
||||
case intstr.String:
|
||||
str := maxConcurrency.StrVal
|
||||
f, err := strconv.ParseFloat(str[:len(str)-1], 64)
|
||||
if err != nil {
|
||||
return length, err
|
||||
}
|
||||
length = int(math.Ceil(f / 100 * float64(total)))
|
||||
case intstr.Int:
|
||||
length = maxConcurrency.IntValue()
|
||||
default:
|
||||
return length, fmt.Errorf("incorrect MaxConcurrency type %v", maxConcurrency.Type)
|
||||
}
|
||||
|
||||
return length, nil
|
||||
}
|
||||
|
||||
func desiredConfigsEqual(a, b addonConfigMap) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
|
||||
for configgrA := range a {
|
||||
if a[configgrA] != b[configgrA] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
467
pkg/addon/controllers/addonconfiguration/graph_test.go
Normal file
467
pkg/addon/controllers/addonconfiguration/graph_test.go
Normal file
@@ -0,0 +1,467 @@
|
||||
package addonconfiguration
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/addonmanager/addontesting"
|
||||
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
)
|
||||
|
||||
type placementDesicion struct {
|
||||
addonv1alpha1.PlacementRef
|
||||
clusters []string
|
||||
}
|
||||
|
||||
func TestConfigurationGraph(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
defaultConfigs []addonv1alpha1.ConfigMeta
|
||||
defaultConfigReference []addonv1alpha1.DefaultConfigReference
|
||||
addons []*addonv1alpha1.ManagedClusterAddOn
|
||||
placementDesicions []placementDesicion
|
||||
placementStrategies []addonv1alpha1.PlacementStrategy
|
||||
installProgressions []addonv1alpha1.InstallProgression
|
||||
expected []*addonNode
|
||||
}{
|
||||
{
|
||||
name: "no output",
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "default config only",
|
||||
defaultConfigs: []addonv1alpha1.ConfigMeta{
|
||||
{ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Bar"}},
|
||||
{ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DefaultConfig: &addonv1alpha1.ConfigReferent{Name: "test"}},
|
||||
},
|
||||
defaultConfigReference: []addonv1alpha1.DefaultConfigReference{
|
||||
newDefaultConfigReference("core", "Foo", "test", "<core-foo-test-hash>"),
|
||||
},
|
||||
addons: []*addonv1alpha1.ManagedClusterAddOn{
|
||||
addontesting.NewAddon("test", "cluster1"),
|
||||
addontesting.NewAddon("test", "cluster2"),
|
||||
},
|
||||
expected: []*addonNode{
|
||||
{
|
||||
desiredConfigs: map[addonv1alpha1.ConfigGroupResource]addonv1alpha1.ConfigReference{
|
||||
{Group: "core", Resource: "Foo"}: {
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
SpecHash: "<core-foo-test-hash>",
|
||||
},
|
||||
},
|
||||
},
|
||||
mca: addontesting.NewAddon("test", "cluster1"),
|
||||
},
|
||||
{
|
||||
desiredConfigs: map[addonv1alpha1.ConfigGroupResource]addonv1alpha1.ConfigReference{
|
||||
{Group: "core", Resource: "Foo"}: {
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
SpecHash: "<core-foo-test-hash>",
|
||||
},
|
||||
},
|
||||
},
|
||||
mca: addontesting.NewAddon("test", "cluster2"),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "with placement strategy",
|
||||
defaultConfigs: []addonv1alpha1.ConfigMeta{
|
||||
{ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Bar"},
|
||||
DefaultConfig: &addonv1alpha1.ConfigReferent{Name: "test"}},
|
||||
{ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DefaultConfig: &addonv1alpha1.ConfigReferent{Name: "test"}},
|
||||
},
|
||||
defaultConfigReference: []addonv1alpha1.DefaultConfigReference{
|
||||
newDefaultConfigReference("core", "Bar", "test", "<core-bar-test-hash>"),
|
||||
newDefaultConfigReference("core", "Foo", "test", "<core-foo-test-hash>"),
|
||||
},
|
||||
addons: []*addonv1alpha1.ManagedClusterAddOn{
|
||||
addontesting.NewAddon("test", "cluster1"),
|
||||
addontesting.NewAddon("test", "cluster2"),
|
||||
addontesting.NewAddon("test", "cluster3"),
|
||||
},
|
||||
placementDesicions: []placementDesicion{
|
||||
{PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"},
|
||||
clusters: []string{"cluster1"}},
|
||||
{PlacementRef: addonv1alpha1.PlacementRef{Name: "placement2", Namespace: "test"},
|
||||
clusters: []string{"cluster2"}},
|
||||
},
|
||||
placementStrategies: []addonv1alpha1.PlacementStrategy{
|
||||
{PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"}},
|
||||
{PlacementRef: addonv1alpha1.PlacementRef{Name: "placement2", Namespace: "test"}},
|
||||
},
|
||||
installProgressions: []addonv1alpha1.InstallProgression{
|
||||
{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"},
|
||||
ConfigReferences: []addonv1alpha1.InstallConfigReference{
|
||||
newInstallConfigReference("core", "Bar", "test1", "<core-bar-test1-hash>"),
|
||||
},
|
||||
},
|
||||
{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "placement2", Namespace: "test"},
|
||||
ConfigReferences: []addonv1alpha1.InstallConfigReference{
|
||||
newInstallConfigReference("core", "Bar", "test2", "<core-bar-test2-hash>"),
|
||||
newInstallConfigReference("core", "Foo", "test2", "<core-foo-test2-hash>"),
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: []*addonNode{
|
||||
{
|
||||
desiredConfigs: map[addonv1alpha1.ConfigGroupResource]addonv1alpha1.ConfigReference{
|
||||
{Group: "core", Resource: "Bar"}: {
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Bar"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "<core-bar-test1-hash>",
|
||||
},
|
||||
},
|
||||
{Group: "core", Resource: "Foo"}: {
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
SpecHash: "<core-foo-test-hash>",
|
||||
},
|
||||
},
|
||||
},
|
||||
mca: addontesting.NewAddon("test", "cluster1"),
|
||||
},
|
||||
{
|
||||
desiredConfigs: map[addonv1alpha1.ConfigGroupResource]addonv1alpha1.ConfigReference{
|
||||
{Group: "core", Resource: "Bar"}: {
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Bar"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test2"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test2"},
|
||||
SpecHash: "<core-bar-test2-hash>",
|
||||
},
|
||||
},
|
||||
{Group: "core", Resource: "Foo"}: {
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test2"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test2"},
|
||||
SpecHash: "<core-foo-test2-hash>",
|
||||
},
|
||||
},
|
||||
},
|
||||
mca: addontesting.NewAddon("test", "cluster2"),
|
||||
},
|
||||
{
|
||||
desiredConfigs: map[addonv1alpha1.ConfigGroupResource]addonv1alpha1.ConfigReference{
|
||||
{Group: "core", Resource: "Bar"}: {
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Bar"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
SpecHash: "<core-bar-test-hash>",
|
||||
},
|
||||
},
|
||||
{Group: "core", Resource: "Foo"}: {
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
SpecHash: "<core-foo-test-hash>",
|
||||
},
|
||||
},
|
||||
},
|
||||
mca: addontesting.NewAddon("test", "cluster3"),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "placement overlap",
|
||||
defaultConfigs: []addonv1alpha1.ConfigMeta{
|
||||
{ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Bar"},
|
||||
DefaultConfig: &addonv1alpha1.ConfigReferent{Name: "test"}},
|
||||
{ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DefaultConfig: &addonv1alpha1.ConfigReferent{Name: "test"}},
|
||||
},
|
||||
defaultConfigReference: []addonv1alpha1.DefaultConfigReference{
|
||||
newDefaultConfigReference("core", "Bar", "test", "<core-bar-test-hash>"),
|
||||
newDefaultConfigReference("core", "Foo", "test", "<core-foo-test-hash>"),
|
||||
},
|
||||
addons: []*addonv1alpha1.ManagedClusterAddOn{
|
||||
addontesting.NewAddon("test", "cluster1"),
|
||||
addontesting.NewAddon("test", "cluster2"),
|
||||
addontesting.NewAddon("test", "cluster3"),
|
||||
},
|
||||
placementStrategies: []addonv1alpha1.PlacementStrategy{
|
||||
{PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"}},
|
||||
{PlacementRef: addonv1alpha1.PlacementRef{Name: "placement2", Namespace: "test"}},
|
||||
},
|
||||
placementDesicions: []placementDesicion{
|
||||
{PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"},
|
||||
clusters: []string{"cluster1", "cluster2"}},
|
||||
{PlacementRef: addonv1alpha1.PlacementRef{Name: "placement2", Namespace: "test"},
|
||||
clusters: []string{"cluster2", "cluster3"}},
|
||||
},
|
||||
installProgressions: []addonv1alpha1.InstallProgression{
|
||||
{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"},
|
||||
ConfigReferences: []addonv1alpha1.InstallConfigReference{
|
||||
newInstallConfigReference("core", "Bar", "test1", "<core-bar-test1-hash>"),
|
||||
},
|
||||
},
|
||||
{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "placement2", Namespace: "test"},
|
||||
ConfigReferences: []addonv1alpha1.InstallConfigReference{
|
||||
newInstallConfigReference("core", "Bar", "test2", "<core-bar-test2-hash>"),
|
||||
newInstallConfigReference("core", "Foo", "test2", "<core-foo-test2-hash>"),
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: []*addonNode{
|
||||
{
|
||||
desiredConfigs: map[addonv1alpha1.ConfigGroupResource]addonv1alpha1.ConfigReference{
|
||||
{Group: "core", Resource: "Bar"}: {
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Bar"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "<core-bar-test1-hash>",
|
||||
},
|
||||
},
|
||||
{Group: "core", Resource: "Foo"}: {
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
SpecHash: "<core-foo-test-hash>",
|
||||
},
|
||||
},
|
||||
},
|
||||
mca: addontesting.NewAddon("test", "cluster1"),
|
||||
},
|
||||
{
|
||||
desiredConfigs: map[addonv1alpha1.ConfigGroupResource]addonv1alpha1.ConfigReference{
|
||||
{Group: "core", Resource: "Bar"}: {
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Bar"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test2"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test2"},
|
||||
SpecHash: "<core-bar-test2-hash>",
|
||||
},
|
||||
},
|
||||
{Group: "core", Resource: "Foo"}: {
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test2"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test2"},
|
||||
SpecHash: "<core-foo-test2-hash>",
|
||||
},
|
||||
},
|
||||
},
|
||||
mca: addontesting.NewAddon("test", "cluster2"),
|
||||
},
|
||||
{
|
||||
desiredConfigs: map[addonv1alpha1.ConfigGroupResource]addonv1alpha1.ConfigReference{
|
||||
{Group: "core", Resource: "Bar"}: {
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Bar"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test2"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test2"},
|
||||
SpecHash: "<core-bar-test2-hash>",
|
||||
},
|
||||
},
|
||||
{Group: "core", Resource: "Foo"}: {
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test2"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test2"},
|
||||
SpecHash: "<core-foo-test2-hash>",
|
||||
},
|
||||
},
|
||||
},
|
||||
mca: addontesting.NewAddon("test", "cluster3"),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "mca override",
|
||||
defaultConfigs: []addonv1alpha1.ConfigMeta{
|
||||
{ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Bar"},
|
||||
DefaultConfig: &addonv1alpha1.ConfigReferent{Name: "test"}},
|
||||
{ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DefaultConfig: &addonv1alpha1.ConfigReferent{Name: "test"}},
|
||||
},
|
||||
defaultConfigReference: []addonv1alpha1.DefaultConfigReference{
|
||||
newDefaultConfigReference("core", "Bar", "test", "<core-bar-test-hash>"),
|
||||
newDefaultConfigReference("core", "Foo", "test", "<core-foo-test-hash>"),
|
||||
},
|
||||
addons: []*addonv1alpha1.ManagedClusterAddOn{
|
||||
newManagedClusterAddon("test", "cluster1", []addonv1alpha1.AddOnConfig{
|
||||
{ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Bar"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"}},
|
||||
}, nil),
|
||||
addontesting.NewAddon("test", "cluster2"),
|
||||
addontesting.NewAddon("test", "cluster3"),
|
||||
},
|
||||
placementStrategies: []addonv1alpha1.PlacementStrategy{
|
||||
{PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"}},
|
||||
{PlacementRef: addonv1alpha1.PlacementRef{Name: "placement2", Namespace: "test"}},
|
||||
},
|
||||
placementDesicions: []placementDesicion{
|
||||
{PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"},
|
||||
clusters: []string{"cluster1"}},
|
||||
{PlacementRef: addonv1alpha1.PlacementRef{Name: "placement2", Namespace: "test"},
|
||||
clusters: []string{"cluster2"}},
|
||||
},
|
||||
installProgressions: []addonv1alpha1.InstallProgression{
|
||||
{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"},
|
||||
ConfigReferences: []addonv1alpha1.InstallConfigReference{
|
||||
newInstallConfigReference("core", "Foo", "test1", "<core-foo-test1-hash>"),
|
||||
},
|
||||
},
|
||||
{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "placement2", Namespace: "test"},
|
||||
ConfigReferences: []addonv1alpha1.InstallConfigReference{
|
||||
newInstallConfigReference("core", "Bar", "test2", "<core-bar-test2-hash>"),
|
||||
newInstallConfigReference("core", "Foo", "test2", "<core-foo-test2-hash>"),
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: []*addonNode{
|
||||
{
|
||||
desiredConfigs: map[addonv1alpha1.ConfigGroupResource]addonv1alpha1.ConfigReference{
|
||||
{Group: "core", Resource: "Bar"}: {
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Bar"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "",
|
||||
},
|
||||
},
|
||||
{Group: "core", Resource: "Foo"}: {
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "<core-foo-test1-hash>",
|
||||
},
|
||||
},
|
||||
},
|
||||
mca: newManagedClusterAddon("test", "cluster1", []addonv1alpha1.AddOnConfig{
|
||||
{ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Bar"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"}},
|
||||
}, nil),
|
||||
},
|
||||
{
|
||||
desiredConfigs: map[addonv1alpha1.ConfigGroupResource]addonv1alpha1.ConfigReference{
|
||||
{Group: "core", Resource: "Bar"}: {
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Bar"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test2"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test2"},
|
||||
SpecHash: "<core-bar-test2-hash>",
|
||||
},
|
||||
},
|
||||
{Group: "core", Resource: "Foo"}: {
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test2"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test2"},
|
||||
SpecHash: "<core-foo-test2-hash>",
|
||||
},
|
||||
},
|
||||
},
|
||||
mca: addontesting.NewAddon("test", "cluster2"),
|
||||
},
|
||||
{
|
||||
desiredConfigs: map[addonv1alpha1.ConfigGroupResource]addonv1alpha1.ConfigReference{
|
||||
{Group: "core", Resource: "Bar"}: {
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Bar"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
SpecHash: "<core-bar-test-hash>",
|
||||
},
|
||||
},
|
||||
{Group: "core", Resource: "Foo"}: {
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
SpecHash: "<core-foo-test-hash>",
|
||||
},
|
||||
},
|
||||
},
|
||||
mca: addontesting.NewAddon("test", "cluster3"),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
graph := newGraph(c.defaultConfigs, c.defaultConfigReference)
|
||||
for _, addon := range c.addons {
|
||||
graph.addAddonNode(addon)
|
||||
}
|
||||
for i, decision := range c.placementDesicions {
|
||||
graph.addPlacementNode(c.placementStrategies[i], c.installProgressions[i], decision.clusters)
|
||||
}
|
||||
|
||||
actual := graph.addonToUpdate()
|
||||
if len(actual) != len(c.expected) {
|
||||
t.Errorf("output length is not correct, expected %v, got %v", len(c.expected), len(actual))
|
||||
}
|
||||
|
||||
for _, ev := range c.expected {
|
||||
compared := false
|
||||
for _, v := range actual {
|
||||
if v == nil || ev == nil {
|
||||
t.Errorf("addonNode should not be nil")
|
||||
}
|
||||
if ev.mca != nil && v.mca != nil && ev.mca.Namespace == v.mca.Namespace {
|
||||
if !reflect.DeepEqual(v, ev) {
|
||||
t.Errorf("output is not correct, cluster %s, expected %v, got %v", v.mca.Namespace, ev, v)
|
||||
}
|
||||
compared = true
|
||||
}
|
||||
}
|
||||
|
||||
if !compared {
|
||||
t.Errorf("not found addonNode %v", ev.mca)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func newInstallConfigReference(group, resource, name, hash string) addonv1alpha1.InstallConfigReference {
|
||||
return addonv1alpha1.InstallConfigReference{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{
|
||||
Group: group,
|
||||
Resource: resource,
|
||||
},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: name},
|
||||
SpecHash: hash,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newDefaultConfigReference(group, resource, name, hash string) addonv1alpha1.DefaultConfigReference {
|
||||
return addonv1alpha1.DefaultConfigReference{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{
|
||||
Group: group,
|
||||
Resource: resource,
|
||||
},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: name},
|
||||
SpecHash: hash,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
package addonconfiguration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
jsonpatch "github.com/evanphx/json-patch"
|
||||
"k8s.io/apimachinery/pkg/api/equality"
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
||||
"k8s.io/klog/v2"
|
||||
|
||||
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
addonv1alpha1client "open-cluster-management.io/api/client/addon/clientset/versioned"
|
||||
)
|
||||
|
||||
type clusterManagementAddonProgressingReconciler struct {
|
||||
addonClient addonv1alpha1client.Interface
|
||||
}
|
||||
|
||||
func (d *clusterManagementAddonProgressingReconciler) reconcile(
|
||||
ctx context.Context, cma *addonv1alpha1.ClusterManagementAddOn, graph *configurationGraph) (*addonv1alpha1.ClusterManagementAddOn, reconcileState, error) {
|
||||
var errs []error
|
||||
cmaCopy := cma.DeepCopy()
|
||||
placementNodes := graph.getPlacementNodes()
|
||||
|
||||
// go through addons and update condition per install progression
|
||||
for i, installProgression := range cmaCopy.Status.InstallProgressions {
|
||||
placementNode, exist := placementNodes[installProgression.PlacementRef]
|
||||
if !exist {
|
||||
continue
|
||||
}
|
||||
|
||||
isUpgrade := false
|
||||
|
||||
for _, configReference := range installProgression.ConfigReferences {
|
||||
if configReference.LastAppliedConfig != nil {
|
||||
isUpgrade = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
setAddOnInstallProgressionsAndLastApplied(&cmaCopy.Status.InstallProgressions[i],
|
||||
isUpgrade,
|
||||
placementNode.addonUpgrading(),
|
||||
placementNode.addonUpgraded(),
|
||||
len(placementNode.clusters),
|
||||
)
|
||||
}
|
||||
|
||||
err := d.patchMgmtAddonStatus(ctx, cmaCopy, cma)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
return cmaCopy, reconcileContinue, utilerrors.NewAggregate(errs)
|
||||
}
|
||||
|
||||
func (d *clusterManagementAddonProgressingReconciler) patchMgmtAddonStatus(ctx context.Context, new, old *addonv1alpha1.ClusterManagementAddOn) error {
|
||||
if equality.Semantic.DeepEqual(new.Status, old.Status) {
|
||||
return nil
|
||||
}
|
||||
|
||||
oldData, err := json.Marshal(&addonv1alpha1.ClusterManagementAddOn{
|
||||
Status: addonv1alpha1.ClusterManagementAddOnStatus{
|
||||
InstallProgressions: old.Status.InstallProgressions,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
newData, err := json.Marshal(&addonv1alpha1.ClusterManagementAddOn{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
UID: new.UID,
|
||||
ResourceVersion: new.ResourceVersion,
|
||||
},
|
||||
Status: addonv1alpha1.ClusterManagementAddOnStatus{
|
||||
InstallProgressions: new.Status.InstallProgressions,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
patchBytes, err := jsonpatch.CreateMergePatch(oldData, newData)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create patch for addon %s: %w", new.Name, err)
|
||||
}
|
||||
|
||||
klog.V(2).Infof("Patching clustermanagementaddon %s status with %s", new.Name, string(patchBytes))
|
||||
_, err = d.addonClient.AddonV1alpha1().ClusterManagementAddOns().Patch(
|
||||
ctx, new.Name, types.MergePatchType, patchBytes, metav1.PatchOptions{}, "status")
|
||||
return err
|
||||
}
|
||||
|
||||
func setAddOnInstallProgressionsAndLastApplied(installProgression *addonv1alpha1.InstallProgression, isUpgrade bool, progressing, done, total int) {
|
||||
// always update progressing condition when there is no config
|
||||
// skip update progressing condition when last applied config already the same as desired
|
||||
skip := len(installProgression.ConfigReferences) > 0
|
||||
for _, configReference := range installProgression.ConfigReferences {
|
||||
if !equality.Semantic.DeepEqual(configReference.LastAppliedConfig, configReference.DesiredConfig) &&
|
||||
!equality.Semantic.DeepEqual(configReference.LastKnownGoodConfig, configReference.DesiredConfig) {
|
||||
skip = false
|
||||
}
|
||||
}
|
||||
if skip {
|
||||
return
|
||||
}
|
||||
condition := metav1.Condition{
|
||||
Type: addonv1alpha1.ManagedClusterAddOnConditionProgressing,
|
||||
}
|
||||
if (total == 0 && done == 0) || (done != total) {
|
||||
condition.Status = metav1.ConditionTrue
|
||||
if isUpgrade {
|
||||
condition.Reason = addonv1alpha1.ProgressingReasonUpgrading
|
||||
condition.Message = fmt.Sprintf("%d/%d upgrading...", progressing+done, total)
|
||||
} else {
|
||||
condition.Reason = addonv1alpha1.ProgressingReasonInstalling
|
||||
condition.Message = fmt.Sprintf("%d/%d installing...", progressing+done, total)
|
||||
}
|
||||
} else {
|
||||
for i, configRef := range installProgression.ConfigReferences {
|
||||
installProgression.ConfigReferences[i].LastAppliedConfig = configRef.DesiredConfig.DeepCopy()
|
||||
installProgression.ConfigReferences[i].LastKnownGoodConfig = configRef.DesiredConfig.DeepCopy()
|
||||
}
|
||||
condition.Status = metav1.ConditionFalse
|
||||
if isUpgrade {
|
||||
condition.Reason = addonv1alpha1.ProgressingReasonUpgradeSucceed
|
||||
condition.Message = fmt.Sprintf("%d/%d upgrade completed with no errors.", done, total)
|
||||
} else {
|
||||
condition.Reason = addonv1alpha1.ProgressingReasonInstallSucceed
|
||||
condition.Message = fmt.Sprintf("%d/%d install completed with no errors.", done, total)
|
||||
}
|
||||
}
|
||||
meta.SetStatusCondition(&installProgression.Conditions, condition)
|
||||
}
|
||||
@@ -0,0 +1,628 @@
|
||||
package addonconfiguration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
apiequality "k8s.io/apimachinery/pkg/api/equality"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
clienttesting "k8s.io/client-go/testing"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/addonmanager/addontesting"
|
||||
"open-cluster-management.io/addon-framework/pkg/index"
|
||||
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
fakeaddon "open-cluster-management.io/api/client/addon/clientset/versioned/fake"
|
||||
addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions"
|
||||
fakecluster "open-cluster-management.io/api/client/cluster/clientset/versioned/fake"
|
||||
clusterv1informers "open-cluster-management.io/api/client/cluster/informers/externalversions"
|
||||
clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
|
||||
)
|
||||
|
||||
func TestMgmtAddonProgressingReconcile(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
managedClusteraddon []runtime.Object
|
||||
clusterManagementAddon []runtime.Object
|
||||
placements []runtime.Object
|
||||
placementDecisions []runtime.Object
|
||||
validateAddonActions func(t *testing.T, actions []clienttesting.Action)
|
||||
expectErr bool
|
||||
}{
|
||||
{
|
||||
name: "no managedClusteraddon",
|
||||
managedClusteraddon: []runtime.Object{},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "", "").
|
||||
WithPlacementStrategy(addonv1alpha1.PlacementStrategy{PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"}}).
|
||||
WithInstallProgression(addonv1alpha1.InstallProgression{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"},
|
||||
ConfigReferences: []addonv1alpha1.InstallConfigReference{
|
||||
{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
},
|
||||
},
|
||||
}).Build()},
|
||||
placements: []runtime.Object{
|
||||
&clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: "placement1", Namespace: "test"}},
|
||||
},
|
||||
placementDecisions: []runtime.Object{
|
||||
&clusterv1beta1.PlacementDecision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "placement1",
|
||||
Namespace: "test",
|
||||
Labels: map[string]string{clusterv1beta1.PlacementLabel: "placement1"},
|
||||
},
|
||||
Status: clusterv1beta1.PlacementDecisionStatus{
|
||||
Decisions: []clusterv1beta1.ClusterDecision{{ClusterName: "cluster1"}, {ClusterName: "cluster2"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch")
|
||||
actual := actions[0].(clienttesting.PatchActionImpl).Patch
|
||||
cma := &addonv1alpha1.ClusterManagementAddOn{}
|
||||
err := json.Unmarshal(actual, cma)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(cma.Status.DefaultConfigReferences) != 0 {
|
||||
t.Errorf("DefaultConfigReferences object is not correct: %v", cma.Status.DefaultConfigReferences)
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].ConfigReferences[0].LastAppliedConfig != nil {
|
||||
t.Errorf("InstallProgressions LastAppliedConfig is not correct: %v", cma.Status.InstallProgressions[0].ConfigReferences[0])
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].ConfigReferences[0].LastKnownGoodConfig != nil {
|
||||
t.Errorf("InstallProgressions LastKnownGoodConfig is not correct: %v", cma.Status.InstallProgressions[0].ConfigReferences[0])
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].Conditions[0].Reason != addonv1alpha1.ProgressingReasonInstalling {
|
||||
t.Errorf("InstallProgressions condition is not correct: %v", cma.Status.InstallProgressions[0].Conditions[0].Reason)
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].Conditions[0].Message != "0/2 installing..." {
|
||||
t.Errorf("InstallProgressions condition is not correct: %v", cma.Status.InstallProgressions[0].Conditions[0].Message)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "no placement",
|
||||
managedClusteraddon: []runtime.Object{},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "", "").WithPlacementStrategy().
|
||||
WithInstallProgression(addonv1alpha1.InstallProgression{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"},
|
||||
ConfigReferences: []addonv1alpha1.InstallConfigReference{
|
||||
{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
},
|
||||
},
|
||||
}).Build()},
|
||||
validateAddonActions: addontesting.AssertNoActions,
|
||||
},
|
||||
{
|
||||
name: "update clustermanagementaddon status with condition Progressing installing",
|
||||
managedClusteraddon: []runtime.Object{func() *addonv1alpha1.ManagedClusterAddOn {
|
||||
addon := addontesting.NewAddon("test", "cluster1")
|
||||
addon.Status.ConfigReferences = []addonv1alpha1.ConfigReference{
|
||||
{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
},
|
||||
}
|
||||
return addon
|
||||
}()},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "", "").
|
||||
WithPlacementStrategy(addonv1alpha1.PlacementStrategy{PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"}}).
|
||||
WithInstallProgression(addonv1alpha1.InstallProgression{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"},
|
||||
ConfigReferences: []addonv1alpha1.InstallConfigReference{
|
||||
{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
},
|
||||
},
|
||||
}).Build()},
|
||||
placements: []runtime.Object{
|
||||
&clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: "placement1", Namespace: "test"}},
|
||||
},
|
||||
placementDecisions: []runtime.Object{
|
||||
&clusterv1beta1.PlacementDecision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "placement1",
|
||||
Namespace: "test",
|
||||
Labels: map[string]string{clusterv1beta1.PlacementLabel: "placement1"},
|
||||
},
|
||||
Status: clusterv1beta1.PlacementDecisionStatus{
|
||||
Decisions: []clusterv1beta1.ClusterDecision{{ClusterName: "cluster1"}, {ClusterName: "cluster2"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch")
|
||||
actual := actions[0].(clienttesting.PatchActionImpl).Patch
|
||||
cma := &addonv1alpha1.ClusterManagementAddOn{}
|
||||
err := json.Unmarshal(actual, cma)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(cma.Status.DefaultConfigReferences) != 0 {
|
||||
t.Errorf("DefaultConfigReferences object is not correct: %v", cma.Status.DefaultConfigReferences)
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].ConfigReferences[0].LastAppliedConfig != nil {
|
||||
t.Errorf("InstallProgressions LastAppliedConfig is not correct: %v", cma.Status.InstallProgressions[0].ConfigReferences[0])
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].ConfigReferences[0].LastKnownGoodConfig != nil {
|
||||
t.Errorf("InstallProgressions LastKnownGoodConfig is not correct: %v", cma.Status.InstallProgressions[0].ConfigReferences[0])
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].Conditions[0].Reason != addonv1alpha1.ProgressingReasonInstalling {
|
||||
t.Errorf("InstallProgressions condition is not correct: %v", cma.Status.InstallProgressions[0].Conditions[0].Reason)
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].Conditions[0].Message != "1/2 installing..." {
|
||||
t.Errorf("InstallProgressions condition is not correct: %v", cma.Status.InstallProgressions[0].Conditions[0].Message)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "update clustermanagementaddon status with condition Progressing install succeed",
|
||||
managedClusteraddon: []runtime.Object{func() *addonv1alpha1.ManagedClusterAddOn {
|
||||
addon := addontesting.NewAddon("test", "cluster1")
|
||||
addon.Status.ConfigReferences = []addonv1alpha1.ConfigReference{
|
||||
{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
LastAppliedConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
},
|
||||
}
|
||||
return addon
|
||||
}()},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "", "").
|
||||
WithPlacementStrategy(addonv1alpha1.PlacementStrategy{PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"}}).
|
||||
WithInstallProgression(addonv1alpha1.InstallProgression{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"},
|
||||
ConfigReferences: []addonv1alpha1.InstallConfigReference{
|
||||
{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
},
|
||||
},
|
||||
}).Build()},
|
||||
placements: []runtime.Object{
|
||||
&clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: "placement1", Namespace: "test"}},
|
||||
},
|
||||
placementDecisions: []runtime.Object{
|
||||
&clusterv1beta1.PlacementDecision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "placement1",
|
||||
Namespace: "test",
|
||||
Labels: map[string]string{clusterv1beta1.PlacementLabel: "placement1"},
|
||||
},
|
||||
Status: clusterv1beta1.PlacementDecisionStatus{
|
||||
Decisions: []clusterv1beta1.ClusterDecision{{ClusterName: "cluster1"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch")
|
||||
actual := actions[0].(clienttesting.PatchActionImpl).Patch
|
||||
cma := &addonv1alpha1.ClusterManagementAddOn{}
|
||||
err := json.Unmarshal(actual, cma)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(cma.Status.DefaultConfigReferences) != 0 {
|
||||
t.Errorf("DefaultConfigReferences object is not correct: %v", cma.Status.DefaultConfigReferences)
|
||||
}
|
||||
if !apiequality.Semantic.DeepEqual(cma.Status.InstallProgressions[0].ConfigReferences[0].LastAppliedConfig, cma.Status.InstallProgressions[0].ConfigReferences[0].DesiredConfig) {
|
||||
t.Errorf("InstallProgressions LastAppliedConfig is not correct: %v", cma.Status.InstallProgressions[0].ConfigReferences[0])
|
||||
}
|
||||
if !apiequality.Semantic.DeepEqual(cma.Status.InstallProgressions[0].ConfigReferences[0].LastKnownGoodConfig, cma.Status.InstallProgressions[0].ConfigReferences[0].DesiredConfig) {
|
||||
t.Errorf("InstallProgressions LastKnownGoodConfig is not correct: %v", cma.Status.InstallProgressions[0].ConfigReferences[0])
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].Conditions[0].Reason != addonv1alpha1.ProgressingReasonInstallSucceed {
|
||||
t.Errorf("InstallProgressions condition is not correct: %v", cma.Status.InstallProgressions[0].Conditions)
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].Conditions[0].Message != "1/1 install completed with no errors." {
|
||||
t.Errorf("InstallProgressions condition is not correct: %v", cma.Status.InstallProgressions[0].Conditions)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "update clustermanagementaddon status with condition Progressing upgrading",
|
||||
managedClusteraddon: []runtime.Object{func() *addonv1alpha1.ManagedClusterAddOn {
|
||||
addon := addontesting.NewAddon("test", "cluster1")
|
||||
addon.Status.ConfigReferences = []addonv1alpha1.ConfigReference{
|
||||
{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
},
|
||||
}
|
||||
return addon
|
||||
}()},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "", "").
|
||||
WithPlacementStrategy(addonv1alpha1.PlacementStrategy{PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"}}).
|
||||
WithInstallProgression(addonv1alpha1.InstallProgression{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"},
|
||||
ConfigReferences: []addonv1alpha1.InstallConfigReference{
|
||||
{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
LastAppliedConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash",
|
||||
},
|
||||
},
|
||||
},
|
||||
}).Build()},
|
||||
placements: []runtime.Object{
|
||||
&clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: "placement1", Namespace: "test"}},
|
||||
},
|
||||
placementDecisions: []runtime.Object{
|
||||
&clusterv1beta1.PlacementDecision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "placement1",
|
||||
Namespace: "test",
|
||||
Labels: map[string]string{clusterv1beta1.PlacementLabel: "placement1"},
|
||||
},
|
||||
Status: clusterv1beta1.PlacementDecisionStatus{
|
||||
Decisions: []clusterv1beta1.ClusterDecision{{ClusterName: "cluster1"}, {ClusterName: "cluster2"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch")
|
||||
actual := actions[0].(clienttesting.PatchActionImpl).Patch
|
||||
cma := &addonv1alpha1.ClusterManagementAddOn{}
|
||||
err := json.Unmarshal(actual, cma)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(cma.Status.DefaultConfigReferences) != 0 {
|
||||
t.Errorf("DefaultConfigReferences object is not correct: %v", cma.Status.DefaultConfigReferences)
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].ConfigReferences[0].LastKnownGoodConfig != nil {
|
||||
t.Errorf("InstallProgressions LastKnownGoodConfig is not correct: %v", cma.Status.InstallProgressions[0].ConfigReferences[0])
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].Conditions[0].Reason != addonv1alpha1.ProgressingReasonUpgrading {
|
||||
t.Errorf("InstallProgressions condition is not correct: %v", cma.Status.InstallProgressions[0].Conditions)
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].Conditions[0].Message != "1/2 upgrading..." {
|
||||
t.Errorf("InstallProgressions condition is not correct: %v", cma.Status.InstallProgressions[0].Conditions)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "update clustermanagementaddon status with condition Progressing upgrade succeed",
|
||||
managedClusteraddon: []runtime.Object{func() *addonv1alpha1.ManagedClusterAddOn {
|
||||
addon := addontesting.NewAddon("test", "cluster1")
|
||||
addon.Status.ConfigReferences = []addonv1alpha1.ConfigReference{
|
||||
{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
LastAppliedConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
},
|
||||
}
|
||||
return addon
|
||||
}()},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "", "").
|
||||
WithPlacementStrategy(addonv1alpha1.PlacementStrategy{PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"}}).
|
||||
WithInstallProgression(addonv1alpha1.InstallProgression{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"},
|
||||
ConfigReferences: []addonv1alpha1.InstallConfigReference{
|
||||
{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
LastAppliedConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash",
|
||||
},
|
||||
},
|
||||
},
|
||||
}).Build()},
|
||||
placements: []runtime.Object{
|
||||
&clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: "placement1", Namespace: "test"}},
|
||||
},
|
||||
placementDecisions: []runtime.Object{
|
||||
&clusterv1beta1.PlacementDecision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "placement1",
|
||||
Namespace: "test",
|
||||
Labels: map[string]string{clusterv1beta1.PlacementLabel: "placement1"},
|
||||
},
|
||||
Status: clusterv1beta1.PlacementDecisionStatus{
|
||||
Decisions: []clusterv1beta1.ClusterDecision{{ClusterName: "cluster1"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch")
|
||||
actual := actions[0].(clienttesting.PatchActionImpl).Patch
|
||||
cma := &addonv1alpha1.ClusterManagementAddOn{}
|
||||
err := json.Unmarshal(actual, cma)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(cma.Status.DefaultConfigReferences) != 0 {
|
||||
t.Errorf("DefaultConfigReferences object is not correct: %v", cma.Status.DefaultConfigReferences)
|
||||
}
|
||||
if !apiequality.Semantic.DeepEqual(cma.Status.InstallProgressions[0].ConfigReferences[0].LastAppliedConfig, cma.Status.InstallProgressions[0].ConfigReferences[0].DesiredConfig) {
|
||||
t.Errorf("InstallProgressions LastAppliedConfig is not correct: %v", cma.Status.InstallProgressions[0].ConfigReferences[0])
|
||||
}
|
||||
if !apiequality.Semantic.DeepEqual(cma.Status.InstallProgressions[0].ConfigReferences[0].LastKnownGoodConfig, cma.Status.InstallProgressions[0].ConfigReferences[0].DesiredConfig) {
|
||||
t.Errorf("InstallProgressions LastKnownGoodConfig is not correct: %v", cma.Status.InstallProgressions[0].ConfigReferences[0])
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].Conditions[0].Reason != addonv1alpha1.ProgressingReasonUpgradeSucceed {
|
||||
t.Errorf("InstallProgressions condition is not correct: %v", cma.Status.InstallProgressions[0].Conditions)
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].Conditions[0].Message != "1/1 upgrade completed with no errors." {
|
||||
t.Errorf("InstallProgressions condition is not correct: %v", cma.Status.InstallProgressions[0].Conditions)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "mca override cma configs",
|
||||
managedClusteraddon: []runtime.Object{func() *addonv1alpha1.ManagedClusterAddOn {
|
||||
addon := addontesting.NewAddon("test", "cluster1")
|
||||
addon.Spec.Configs = []addonv1alpha1.AddOnConfig{
|
||||
{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "testmca"},
|
||||
},
|
||||
}
|
||||
addon.Status.ConfigReferences = []addonv1alpha1.ConfigReference{
|
||||
{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "testmca"},
|
||||
SpecHash: "hashmca",
|
||||
},
|
||||
LastAppliedConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "testmca"},
|
||||
SpecHash: "hashmca",
|
||||
},
|
||||
},
|
||||
}
|
||||
return addon
|
||||
}()},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "", "").
|
||||
WithPlacementStrategy(addonv1alpha1.PlacementStrategy{PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"}}).
|
||||
WithInstallProgression(addonv1alpha1.InstallProgression{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"},
|
||||
ConfigReferences: []addonv1alpha1.InstallConfigReference{
|
||||
{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
LastAppliedConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash",
|
||||
},
|
||||
},
|
||||
},
|
||||
}).Build()},
|
||||
placements: []runtime.Object{
|
||||
&clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: "placement1", Namespace: "test"}},
|
||||
},
|
||||
placementDecisions: []runtime.Object{
|
||||
&clusterv1beta1.PlacementDecision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "placement1",
|
||||
Namespace: "test",
|
||||
Labels: map[string]string{clusterv1beta1.PlacementLabel: "placement1"},
|
||||
},
|
||||
Status: clusterv1beta1.PlacementDecisionStatus{
|
||||
Decisions: []clusterv1beta1.ClusterDecision{{ClusterName: "cluster1"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch")
|
||||
actual := actions[0].(clienttesting.PatchActionImpl).Patch
|
||||
cma := &addonv1alpha1.ClusterManagementAddOn{}
|
||||
err := json.Unmarshal(actual, cma)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(cma.Status.DefaultConfigReferences) != 0 {
|
||||
t.Errorf("DefaultConfigReferences object is not correct: %v", cma.Status.DefaultConfigReferences)
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].ConfigReferences[0].LastKnownGoodConfig != nil {
|
||||
t.Errorf("InstallProgressions LastKnownGoodConfig is not correct: %v", cma.Status.InstallProgressions[0].ConfigReferences[0])
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].Conditions[0].Reason != addonv1alpha1.ProgressingReasonUpgrading {
|
||||
t.Errorf("InstallProgressions condition is not correct: %v", cma.Status.InstallProgressions[0].Conditions)
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].Conditions[0].Message != "0/1 upgrading..." {
|
||||
t.Errorf("InstallProgressions condition is not correct: %v", cma.Status.InstallProgressions[0].Conditions)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "update clustermanagementaddon status with condition Progressing ConfigurationUnsupported",
|
||||
managedClusteraddon: []runtime.Object{func() *addonv1alpha1.ManagedClusterAddOn {
|
||||
addon := addontesting.NewAddon("test", "cluster1")
|
||||
addon.Status.ConfigReferences = []addonv1alpha1.ConfigReference{
|
||||
{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
},
|
||||
}
|
||||
return addon
|
||||
}()},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "", "").
|
||||
WithPlacementStrategy(addonv1alpha1.PlacementStrategy{PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"}}).
|
||||
WithInstallProgression(addonv1alpha1.InstallProgression{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "placement1", Namespace: "test"},
|
||||
ConfigReferences: []addonv1alpha1.InstallConfigReference{
|
||||
{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{Group: "core", Resource: "Foo"},
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonv1alpha1.ConfigReferent{Name: "test1"},
|
||||
SpecHash: "hash1",
|
||||
},
|
||||
},
|
||||
},
|
||||
}).Build()},
|
||||
placements: []runtime.Object{
|
||||
&clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: "placement1", Namespace: "test"}},
|
||||
},
|
||||
placementDecisions: []runtime.Object{
|
||||
&clusterv1beta1.PlacementDecision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "placement1",
|
||||
Namespace: "test",
|
||||
Labels: map[string]string{clusterv1beta1.PlacementLabel: "placement1"},
|
||||
},
|
||||
Status: clusterv1beta1.PlacementDecisionStatus{
|
||||
Decisions: []clusterv1beta1.ClusterDecision{{ClusterName: "cluster1"}, {ClusterName: "cluster2"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch")
|
||||
actual := actions[0].(clienttesting.PatchActionImpl).Patch
|
||||
cma := &addonv1alpha1.ClusterManagementAddOn{}
|
||||
err := json.Unmarshal(actual, cma)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(cma.Status.DefaultConfigReferences) != 0 {
|
||||
t.Errorf("DefaultConfigReferences object is not correct: %v", cma.Status.DefaultConfigReferences)
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].ConfigReferences[0].LastAppliedConfig != nil {
|
||||
t.Errorf("InstallProgressions LastAppliedConfig is not correct: %v", cma.Status.InstallProgressions[0].ConfigReferences[0])
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].ConfigReferences[0].LastKnownGoodConfig != nil {
|
||||
t.Errorf("InstallProgressions LastKnownGoodConfig is not correct: %v", cma.Status.InstallProgressions[0].ConfigReferences[0])
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].Conditions[0].Reason != addonv1alpha1.ProgressingReasonInstalling {
|
||||
t.Errorf("InstallProgressions condition is not correct: %v", cma.Status.InstallProgressions[0].Conditions)
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].Conditions[0].Message != "1/2 installing..." {
|
||||
t.Errorf("InstallProgressions condition is not correct: %v", cma.Status.InstallProgressions[0].Conditions[0].Message)
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
obj := append(c.clusterManagementAddon, c.managedClusteraddon...)
|
||||
clusterObj := append(c.placements, c.placementDecisions...)
|
||||
fakeClusterClient := fakecluster.NewSimpleClientset(clusterObj...)
|
||||
fakeAddonClient := fakeaddon.NewSimpleClientset(obj...)
|
||||
|
||||
addonInformers := addoninformers.NewSharedInformerFactory(fakeAddonClient, 10*time.Minute)
|
||||
clusterInformers := clusterv1informers.NewSharedInformerFactory(fakeClusterClient, 10*time.Minute)
|
||||
|
||||
err := addonInformers.Addon().V1alpha1().ManagedClusterAddOns().Informer().AddIndexers(
|
||||
cache.Indexers{
|
||||
index.ManagedClusterAddonByName: index.IndexManagedClusterAddonByName,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, obj := range c.placements {
|
||||
if err := clusterInformers.Cluster().V1beta1().Placements().Informer().GetStore().Add(obj); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, obj := range c.placementDecisions {
|
||||
if err := clusterInformers.Cluster().V1beta1().PlacementDecisions().Informer().GetStore().Add(obj); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, obj := range c.clusterManagementAddon {
|
||||
if err = addonInformers.Addon().V1alpha1().ClusterManagementAddOns().Informer().GetStore().Add(obj); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, obj := range c.managedClusteraddon {
|
||||
if err := addonInformers.Addon().V1alpha1().ManagedClusterAddOns().Informer().GetStore().Add(obj); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
controller := &addonConfigurationController{
|
||||
addonClient: fakeAddonClient,
|
||||
placementDecisionLister: clusterInformers.Cluster().V1beta1().PlacementDecisions().Lister(),
|
||||
placementLister: clusterInformers.Cluster().V1beta1().Placements().Lister(),
|
||||
clusterManagementAddonLister: addonInformers.Addon().V1alpha1().ClusterManagementAddOns().Lister(),
|
||||
managedClusterAddonIndexer: addonInformers.Addon().V1alpha1().ManagedClusterAddOns().Informer().GetIndexer(),
|
||||
}
|
||||
|
||||
reconcile := &clusterManagementAddonProgressingReconciler{
|
||||
addonClient: fakeAddonClient,
|
||||
}
|
||||
|
||||
for _, obj := range c.clusterManagementAddon {
|
||||
graph, err := controller.buildConfigurationGraph(obj.(*addonv1alpha1.ClusterManagementAddOn))
|
||||
if err != nil {
|
||||
t.Errorf("expected no error when build graph: %v", err)
|
||||
}
|
||||
_, _, err = reconcile.reconcile(context.TODO(), obj.(*addonv1alpha1.ClusterManagementAddOn), graph)
|
||||
if err != nil && !c.expectErr {
|
||||
t.Errorf("expected no error when sync: %v", err)
|
||||
}
|
||||
if err == nil && c.expectErr {
|
||||
t.Errorf("Expect error but got no error")
|
||||
}
|
||||
}
|
||||
|
||||
c.validateAddonActions(t, fakeAddonClient.Actions())
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package addonmanagement
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/openshift/library-go/pkg/controller/factory"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
"k8s.io/klog/v2"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/index"
|
||||
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
addonv1alpha1client "open-cluster-management.io/api/client/addon/clientset/versioned"
|
||||
clusterlisterv1beta1 "open-cluster-management.io/api/client/cluster/listers/cluster/v1beta1"
|
||||
clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
|
||||
)
|
||||
|
||||
type managedClusterAddonInstallReconciler struct {
|
||||
addonClient addonv1alpha1client.Interface
|
||||
managedClusterAddonIndexer cache.Indexer
|
||||
placementLister clusterlisterv1beta1.PlacementLister
|
||||
placementDecisionLister clusterlisterv1beta1.PlacementDecisionLister
|
||||
addonFilterFunc factory.EventFilterFunc
|
||||
}
|
||||
|
||||
func (d *managedClusterAddonInstallReconciler) reconcile(
|
||||
ctx context.Context, cma *addonv1alpha1.ClusterManagementAddOn) (*addonv1alpha1.ClusterManagementAddOn, reconcileState, error) {
|
||||
// skip apply install strategy for self-managed addon
|
||||
// this is to avoid conflict when addon also define WithInstallStrategy()
|
||||
// the filter will be removed after WithInstallStrategy() is removed from framework.
|
||||
if !d.addonFilterFunc(cma) {
|
||||
return cma, reconcileContinue, nil
|
||||
}
|
||||
|
||||
if cma.Spec.InstallStrategy.Type == "" || cma.Spec.InstallStrategy.Type == addonv1alpha1.AddonInstallStrategyManual {
|
||||
return cma, reconcileContinue, nil
|
||||
}
|
||||
|
||||
addons, err := d.managedClusterAddonIndexer.ByIndex(index.ManagedClusterAddonByName, cma.Name)
|
||||
if err != nil {
|
||||
return cma, reconcileContinue, err
|
||||
}
|
||||
|
||||
existingDeployed := sets.Set[string]{}
|
||||
for _, addonObject := range addons {
|
||||
addon := addonObject.(*addonv1alpha1.ManagedClusterAddOn)
|
||||
existingDeployed.Insert(addon.Namespace)
|
||||
}
|
||||
|
||||
requiredDeployed, err := d.getAllDecisions(cma.Name, cma.Spec.InstallStrategy.Placements)
|
||||
if err != nil {
|
||||
return cma, reconcileContinue, err
|
||||
}
|
||||
|
||||
owner := metav1.NewControllerRef(cma, addonv1alpha1.GroupVersion.WithKind("ClusterManagementAddOn"))
|
||||
toAdd := requiredDeployed.Difference(existingDeployed)
|
||||
toRemove := existingDeployed.Difference(requiredDeployed)
|
||||
|
||||
var errs []error
|
||||
for cluster := range toAdd {
|
||||
_, err := d.addonClient.AddonV1alpha1().ManagedClusterAddOns(cluster).Create(ctx, &addonv1alpha1.ManagedClusterAddOn{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: cma.Name,
|
||||
Namespace: cluster,
|
||||
OwnerReferences: []metav1.OwnerReference{*owner},
|
||||
},
|
||||
Spec: addonv1alpha1.ManagedClusterAddOnSpec{},
|
||||
}, metav1.CreateOptions{})
|
||||
|
||||
if err != nil && !errors.IsAlreadyExists(err) {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
|
||||
for cluster := range toRemove {
|
||||
err := d.addonClient.AddonV1alpha1().ManagedClusterAddOns(cluster).Delete(ctx, cma.Name, metav1.DeleteOptions{})
|
||||
if err != nil && !errors.IsNotFound(err) {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
|
||||
return cma, reconcileContinue, utilerrors.NewAggregate(errs)
|
||||
}
|
||||
|
||||
func (d *managedClusterAddonInstallReconciler) getAllDecisions(addonName string, placements []addonv1alpha1.PlacementStrategy) (sets.Set[string], error) {
|
||||
var errs []error
|
||||
required := sets.Set[string]{}
|
||||
for _, strategy := range placements {
|
||||
_, err := d.placementLister.Placements(strategy.PlacementRef.Namespace).Get(strategy.PlacementRef.Name)
|
||||
if errors.IsNotFound(err) {
|
||||
klog.V(2).Infof("placement %s/%s is not found for addon %s", strategy.PlacementRef.Namespace, strategy.PlacementRef.Name, addonName)
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
continue
|
||||
}
|
||||
|
||||
decisionSelector := labels.SelectorFromSet(labels.Set{
|
||||
clusterv1beta1.PlacementLabel: strategy.PlacementRef.Name,
|
||||
})
|
||||
decisions, err := d.placementDecisionLister.PlacementDecisions(strategy.PlacementRef.Namespace).List(decisionSelector)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, d := range decisions {
|
||||
for _, sd := range d.Status.Decisions {
|
||||
required.Insert(sd.ClusterName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return required, utilerrors.NewAggregate(errs)
|
||||
}
|
||||
117
pkg/addon/controllers/addonmanagement/controller.go
Normal file
117
pkg/addon/controllers/addonmanagement/controller.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package addonmanagement
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/openshift/library-go/pkg/controller/factory"
|
||||
"github.com/openshift/library-go/pkg/operator/events"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
"k8s.io/klog/v2"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/index"
|
||||
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
addonv1alpha1client "open-cluster-management.io/api/client/addon/clientset/versioned"
|
||||
addoninformerv1alpha1 "open-cluster-management.io/api/client/addon/informers/externalversions/addon/v1alpha1"
|
||||
addonlisterv1alpha1 "open-cluster-management.io/api/client/addon/listers/addon/v1alpha1"
|
||||
clusterinformersv1beta1 "open-cluster-management.io/api/client/cluster/informers/externalversions/cluster/v1beta1"
|
||||
)
|
||||
|
||||
type addonManagementController struct {
|
||||
addonClient addonv1alpha1client.Interface
|
||||
clusterManagementAddonLister addonlisterv1alpha1.ClusterManagementAddOnLister
|
||||
clusterManagementAddonIndexer cache.Indexer
|
||||
|
||||
reconcilers []addonManagementReconcile
|
||||
}
|
||||
|
||||
// addonManagementReconcile is a interface for reconcile logic. It creates ManagedClusterAddon based on install strategy
|
||||
type addonManagementReconcile interface {
|
||||
reconcile(ctx context.Context, cma *addonv1alpha1.ClusterManagementAddOn) (*addonv1alpha1.ClusterManagementAddOn, reconcileState, error)
|
||||
}
|
||||
|
||||
type reconcileState int64
|
||||
|
||||
const (
|
||||
reconcileStop reconcileState = iota
|
||||
reconcileContinue
|
||||
)
|
||||
|
||||
func NewAddonManagementController(
|
||||
addonClient addonv1alpha1client.Interface,
|
||||
addonInformers addoninformerv1alpha1.ManagedClusterAddOnInformer,
|
||||
clusterManagementAddonInformers addoninformerv1alpha1.ClusterManagementAddOnInformer,
|
||||
placementInformer clusterinformersv1beta1.PlacementInformer,
|
||||
placementDecisionInformer clusterinformersv1beta1.PlacementDecisionInformer,
|
||||
addonFilterFunc factory.EventFilterFunc,
|
||||
recorder events.Recorder,
|
||||
) factory.Controller {
|
||||
c := &addonManagementController{
|
||||
addonClient: addonClient,
|
||||
clusterManagementAddonLister: clusterManagementAddonInformers.Lister(),
|
||||
clusterManagementAddonIndexer: clusterManagementAddonInformers.Informer().GetIndexer(),
|
||||
|
||||
reconcilers: []addonManagementReconcile{
|
||||
&managedClusterAddonInstallReconciler{
|
||||
addonClient: addonClient,
|
||||
placementDecisionLister: placementDecisionInformer.Lister(),
|
||||
placementLister: placementInformer.Lister(),
|
||||
managedClusterAddonIndexer: addonInformers.Informer().GetIndexer(),
|
||||
addonFilterFunc: addonFilterFunc,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return factory.New().WithInformersQueueKeysFunc(
|
||||
func(obj runtime.Object) []string {
|
||||
key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
|
||||
return []string{key}
|
||||
},
|
||||
addonInformers.Informer(), clusterManagementAddonInformers.Informer()).
|
||||
WithInformersQueueKeysFunc(
|
||||
index.ClusterManagementAddonByPlacementDecisionQueueKey(
|
||||
clusterManagementAddonInformers),
|
||||
placementDecisionInformer.Informer()).
|
||||
WithInformersQueueKeysFunc(
|
||||
index.ClusterManagementAddonByPlacementQueueKey(
|
||||
clusterManagementAddonInformers),
|
||||
placementInformer.Informer()).
|
||||
WithSync(c.sync).ToController("addon-management-controller", recorder)
|
||||
}
|
||||
|
||||
func (c *addonManagementController) sync(ctx context.Context, syncCtx factory.SyncContext) error {
|
||||
key := syncCtx.QueueKey()
|
||||
_, addonName, err := cache.SplitMetaNamespaceKey(key)
|
||||
if err != nil {
|
||||
// ignore addon whose key is invalid
|
||||
return nil
|
||||
}
|
||||
|
||||
klog.V(4).Infof("Reconciling addon %q", addonName)
|
||||
|
||||
cma, err := c.clusterManagementAddonLister.Get(addonName)
|
||||
switch {
|
||||
case errors.IsNotFound(err):
|
||||
return nil
|
||||
case err != nil:
|
||||
return err
|
||||
}
|
||||
|
||||
cma = cma.DeepCopy()
|
||||
|
||||
var state reconcileState
|
||||
var errs []error
|
||||
for _, reconciler := range c.reconcilers {
|
||||
cma, state, err = reconciler.reconcile(ctx, cma)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
if state == reconcileStop {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return utilerrors.NewAggregate(errs)
|
||||
}
|
||||
274
pkg/addon/controllers/addonmanagement/controller_test.go
Normal file
274
pkg/addon/controllers/addonmanagement/controller_test.go
Normal file
@@ -0,0 +1,274 @@
|
||||
package addonmanagement
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
clienttesting "k8s.io/client-go/testing"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/addonmanager/addontesting"
|
||||
"open-cluster-management.io/addon-framework/pkg/agent"
|
||||
"open-cluster-management.io/addon-framework/pkg/index"
|
||||
"open-cluster-management.io/addon-framework/pkg/utils"
|
||||
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
fakeaddon "open-cluster-management.io/api/client/addon/clientset/versioned/fake"
|
||||
addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions"
|
||||
fakecluster "open-cluster-management.io/api/client/cluster/clientset/versioned/fake"
|
||||
clusterv1informers "open-cluster-management.io/api/client/cluster/informers/externalversions"
|
||||
clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
|
||||
)
|
||||
|
||||
func TestAddonInstallReconcile(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
managedClusteraddon []runtime.Object
|
||||
clusterManagementAddon *addonv1alpha1.ClusterManagementAddOn
|
||||
placements []runtime.Object
|
||||
placementDecisions []runtime.Object
|
||||
validateAddonActions func(t *testing.T, actions []clienttesting.Action)
|
||||
expectErr bool
|
||||
}{
|
||||
{
|
||||
name: "no installStrategy",
|
||||
managedClusteraddon: []runtime.Object{},
|
||||
clusterManagementAddon: addontesting.NewClusterManagementAddon("test", "", "").Build(),
|
||||
placements: []runtime.Object{},
|
||||
placementDecisions: []runtime.Object{},
|
||||
validateAddonActions: addontesting.AssertNoActions,
|
||||
},
|
||||
{
|
||||
name: "manual installStrategy",
|
||||
managedClusteraddon: []runtime.Object{},
|
||||
clusterManagementAddon: func() *addonv1alpha1.ClusterManagementAddOn {
|
||||
addon := addontesting.NewClusterManagementAddon("test", "", "").Build()
|
||||
addon.Spec.InstallStrategy = addonv1alpha1.InstallStrategy{
|
||||
Type: addonv1alpha1.AddonInstallStrategyManual,
|
||||
}
|
||||
return addon
|
||||
}(),
|
||||
placements: []runtime.Object{},
|
||||
placementDecisions: []runtime.Object{},
|
||||
validateAddonActions: addontesting.AssertNoActions,
|
||||
},
|
||||
{
|
||||
name: "placement is missting",
|
||||
managedClusteraddon: []runtime.Object{},
|
||||
clusterManagementAddon: func() *addonv1alpha1.ClusterManagementAddOn {
|
||||
addon := addontesting.NewClusterManagementAddon("test", "", "").Build()
|
||||
addon.Spec.InstallStrategy = addonv1alpha1.InstallStrategy{
|
||||
Type: addonv1alpha1.AddonInstallStrategyPlacements,
|
||||
Placements: []addonv1alpha1.PlacementStrategy{
|
||||
{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "test-placement", Namespace: "default"},
|
||||
},
|
||||
},
|
||||
}
|
||||
return addon
|
||||
}(),
|
||||
placements: []runtime.Object{},
|
||||
placementDecisions: []runtime.Object{},
|
||||
validateAddonActions: addontesting.AssertNoActions,
|
||||
},
|
||||
{
|
||||
name: "placement decision is missting",
|
||||
managedClusteraddon: []runtime.Object{},
|
||||
clusterManagementAddon: func() *addonv1alpha1.ClusterManagementAddOn {
|
||||
addon := addontesting.NewClusterManagementAddon("test", "", "").Build()
|
||||
addon.Spec.InstallStrategy = addonv1alpha1.InstallStrategy{
|
||||
Type: addonv1alpha1.AddonInstallStrategyPlacements,
|
||||
Placements: []addonv1alpha1.PlacementStrategy{
|
||||
{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "test-placement", Namespace: "default"},
|
||||
},
|
||||
},
|
||||
}
|
||||
return addon
|
||||
}(),
|
||||
placements: []runtime.Object{
|
||||
&clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: "test-placement", Namespace: "default"}},
|
||||
},
|
||||
placementDecisions: []runtime.Object{},
|
||||
validateAddonActions: addontesting.AssertNoActions,
|
||||
},
|
||||
{
|
||||
name: "install addon",
|
||||
managedClusteraddon: []runtime.Object{},
|
||||
clusterManagementAddon: func() *addonv1alpha1.ClusterManagementAddOn {
|
||||
addon := addontesting.NewClusterManagementAddon("test", "", "").Build()
|
||||
addon.Spec.InstallStrategy = addonv1alpha1.InstallStrategy{
|
||||
Type: addonv1alpha1.AddonInstallStrategyPlacements,
|
||||
Placements: []addonv1alpha1.PlacementStrategy{
|
||||
{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "test-placement", Namespace: "default"},
|
||||
},
|
||||
},
|
||||
}
|
||||
return addon
|
||||
}(),
|
||||
placements: []runtime.Object{
|
||||
&clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: "test-placement", Namespace: "default"}},
|
||||
},
|
||||
placementDecisions: []runtime.Object{
|
||||
&clusterv1beta1.PlacementDecision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-placement",
|
||||
Namespace: "default",
|
||||
Labels: map[string]string{clusterv1beta1.PlacementLabel: "test-placement"},
|
||||
},
|
||||
Status: clusterv1beta1.PlacementDecisionStatus{
|
||||
Decisions: []clusterv1beta1.ClusterDecision{{ClusterName: "cluster1"}, {ClusterName: "cluster2"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "create", "create")
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "addon/remove addon",
|
||||
managedClusteraddon: []runtime.Object{
|
||||
addontesting.NewAddon("test", "cluster0"),
|
||||
addontesting.NewAddon("test", "cluster1"),
|
||||
},
|
||||
clusterManagementAddon: func() *addonv1alpha1.ClusterManagementAddOn {
|
||||
addon := addontesting.NewClusterManagementAddon("test", "", "").Build()
|
||||
addon.Spec.InstallStrategy = addonv1alpha1.InstallStrategy{
|
||||
Type: addonv1alpha1.AddonInstallStrategyPlacements,
|
||||
Placements: []addonv1alpha1.PlacementStrategy{
|
||||
{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "test-placement", Namespace: "default"},
|
||||
},
|
||||
},
|
||||
}
|
||||
return addon
|
||||
}(),
|
||||
placements: []runtime.Object{
|
||||
&clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: "test-placement", Namespace: "default"}},
|
||||
},
|
||||
placementDecisions: []runtime.Object{
|
||||
&clusterv1beta1.PlacementDecision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-placement",
|
||||
Namespace: "default",
|
||||
Labels: map[string]string{clusterv1beta1.PlacementLabel: "test-placement"},
|
||||
},
|
||||
Status: clusterv1beta1.PlacementDecisionStatus{
|
||||
Decisions: []clusterv1beta1.ClusterDecision{{ClusterName: "cluster1"}, {ClusterName: "cluster2"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "create", "delete")
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "multiple placements",
|
||||
managedClusteraddon: []runtime.Object{
|
||||
addontesting.NewAddon("test", "cluster0"),
|
||||
addontesting.NewAddon("test", "cluster1"),
|
||||
},
|
||||
clusterManagementAddon: func() *addonv1alpha1.ClusterManagementAddOn {
|
||||
addon := addontesting.NewClusterManagementAddon("test", "", "").Build()
|
||||
addon.Spec.InstallStrategy = addonv1alpha1.InstallStrategy{
|
||||
Type: addonv1alpha1.AddonInstallStrategyPlacements,
|
||||
Placements: []addonv1alpha1.PlacementStrategy{
|
||||
{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "test-placement", Namespace: "default"},
|
||||
},
|
||||
{
|
||||
PlacementRef: addonv1alpha1.PlacementRef{Name: "test-placement1", Namespace: "default"},
|
||||
},
|
||||
},
|
||||
}
|
||||
return addon
|
||||
}(),
|
||||
placements: []runtime.Object{
|
||||
&clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: "test-placement", Namespace: "default"}},
|
||||
&clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: "test-placement1", Namespace: "default"}},
|
||||
},
|
||||
placementDecisions: []runtime.Object{
|
||||
&clusterv1beta1.PlacementDecision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-placement",
|
||||
Namespace: "default",
|
||||
Labels: map[string]string{clusterv1beta1.PlacementLabel: "test-placement"},
|
||||
},
|
||||
Status: clusterv1beta1.PlacementDecisionStatus{
|
||||
Decisions: []clusterv1beta1.ClusterDecision{{ClusterName: "cluster1"}, {ClusterName: "cluster2"}},
|
||||
},
|
||||
},
|
||||
&clusterv1beta1.PlacementDecision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-placement1",
|
||||
Namespace: "default",
|
||||
Labels: map[string]string{clusterv1beta1.PlacementLabel: "test-placement1"},
|
||||
},
|
||||
Status: clusterv1beta1.PlacementDecisionStatus{
|
||||
Decisions: []clusterv1beta1.ClusterDecision{{ClusterName: "cluster2"}, {ClusterName: "cluster3"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "create", "create", "delete")
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
clusterObj := append(c.placements, c.placementDecisions...)
|
||||
fakeClusterClient := fakecluster.NewSimpleClientset(clusterObj...)
|
||||
fakeAddonClient := fakeaddon.NewSimpleClientset(c.managedClusteraddon...)
|
||||
|
||||
addonInformers := addoninformers.NewSharedInformerFactory(fakeAddonClient, 10*time.Minute)
|
||||
clusterInformers := clusterv1informers.NewSharedInformerFactory(fakeClusterClient, 10*time.Minute)
|
||||
|
||||
err := addonInformers.Addon().V1alpha1().ManagedClusterAddOns().Informer().AddIndexers(
|
||||
cache.Indexers{
|
||||
index.ManagedClusterAddonByName: index.IndexManagedClusterAddonByName,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, obj := range c.placements {
|
||||
if err := clusterInformers.Cluster().V1beta1().Placements().Informer().GetStore().Add(obj); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, obj := range c.placementDecisions {
|
||||
if err := clusterInformers.Cluster().V1beta1().PlacementDecisions().Informer().GetStore().Add(obj); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, obj := range c.managedClusteraddon {
|
||||
if err := addonInformers.Addon().V1alpha1().ManagedClusterAddOns().Informer().GetStore().Add(obj); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
reconcile := &managedClusterAddonInstallReconciler{
|
||||
addonClient: fakeAddonClient,
|
||||
placementLister: clusterInformers.Cluster().V1beta1().Placements().Lister(),
|
||||
placementDecisionLister: clusterInformers.Cluster().V1beta1().PlacementDecisions().Lister(),
|
||||
managedClusterAddonIndexer: addonInformers.Addon().V1alpha1().ManagedClusterAddOns().Informer().GetIndexer(),
|
||||
addonFilterFunc: utils.ManagedBySelf(map[string]agent.AgentAddon{"test": nil}),
|
||||
}
|
||||
|
||||
_, _, err = reconcile.reconcile(context.TODO(), c.clusterManagementAddon)
|
||||
if err != nil && !c.expectErr {
|
||||
t.Errorf("expected no error when sync: %v", err)
|
||||
}
|
||||
if err == nil && c.expectErr {
|
||||
t.Errorf("Expect error but got no error")
|
||||
}
|
||||
c.validateAddonActions(t, fakeAddonClient.Actions())
|
||||
})
|
||||
}
|
||||
}
|
||||
105
pkg/addon/controllers/addonowner/controller.go
Normal file
105
pkg/addon/controllers/addonowner/controller.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package addonowner
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/openshift/library-go/pkg/controller/factory"
|
||||
"github.com/openshift/library-go/pkg/operator/events"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
"k8s.io/klog/v2"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/utils"
|
||||
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
addonv1alpha1client "open-cluster-management.io/api/client/addon/clientset/versioned"
|
||||
addoninformerv1alpha1 "open-cluster-management.io/api/client/addon/informers/externalversions/addon/v1alpha1"
|
||||
addonlisterv1alpha1 "open-cluster-management.io/api/client/addon/listers/addon/v1alpha1"
|
||||
)
|
||||
|
||||
const UnsupportedConfigurationType = "UnsupportedConfiguration"
|
||||
|
||||
// addonOwnerController reconciles instances of managedclusteradd on the hub
|
||||
// to add related ClusterManagementAddon as the owner.
|
||||
type addonOwnerController struct {
|
||||
addonClient addonv1alpha1client.Interface
|
||||
managedClusterAddonLister addonlisterv1alpha1.ManagedClusterAddOnLister
|
||||
clusterManagementAddonLister addonlisterv1alpha1.ClusterManagementAddOnLister
|
||||
addonFilterFunc factory.EventFilterFunc
|
||||
}
|
||||
|
||||
func NewAddonOwnerController(
|
||||
addonClient addonv1alpha1client.Interface,
|
||||
addonInformers addoninformerv1alpha1.ManagedClusterAddOnInformer,
|
||||
clusterManagementAddonInformers addoninformerv1alpha1.ClusterManagementAddOnInformer,
|
||||
addonFilterFunc factory.EventFilterFunc,
|
||||
recorder events.Recorder,
|
||||
) factory.Controller {
|
||||
c := &addonOwnerController{
|
||||
addonClient: addonClient,
|
||||
managedClusterAddonLister: addonInformers.Lister(),
|
||||
clusterManagementAddonLister: clusterManagementAddonInformers.Lister(),
|
||||
addonFilterFunc: addonFilterFunc,
|
||||
}
|
||||
|
||||
return factory.New().
|
||||
WithFilteredEventsInformersQueueKeysFunc(
|
||||
func(obj runtime.Object) []string {
|
||||
key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
|
||||
return []string{key}
|
||||
},
|
||||
c.addonFilterFunc, clusterManagementAddonInformers.Informer()).
|
||||
WithInformersQueueKeysFunc(
|
||||
func(obj runtime.Object) []string {
|
||||
key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
|
||||
return []string{key}
|
||||
},
|
||||
addonInformers.Informer()).
|
||||
WithSync(c.sync).
|
||||
ToController("addon-owner-controller", recorder)
|
||||
}
|
||||
|
||||
func (c *addonOwnerController) sync(ctx context.Context, syncCtx factory.SyncContext) error {
|
||||
key := syncCtx.QueueKey()
|
||||
klog.V(4).Infof("Reconciling addon %q", key)
|
||||
|
||||
namespace, addonName, err := cache.SplitMetaNamespaceKey(key)
|
||||
if err != nil {
|
||||
// ignore addon whose key is invalid
|
||||
return nil
|
||||
}
|
||||
|
||||
addon, err := c.managedClusterAddonLister.ManagedClusterAddOns(namespace).Get(addonName)
|
||||
switch {
|
||||
case errors.IsNotFound(err):
|
||||
return nil
|
||||
case err != nil:
|
||||
return err
|
||||
}
|
||||
|
||||
addonCopy := addon.DeepCopy()
|
||||
modified := false
|
||||
|
||||
clusterManagementAddon, err := c.clusterManagementAddonLister.Get(addonName)
|
||||
if errors.IsNotFound(err) {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !c.addonFilterFunc(clusterManagementAddon) {
|
||||
return nil
|
||||
}
|
||||
|
||||
owner := metav1.NewControllerRef(clusterManagementAddon, addonapiv1alpha1.GroupVersion.WithKind("ClusterManagementAddOn"))
|
||||
modified = utils.MergeOwnerRefs(&addonCopy.OwnerReferences, *owner, false)
|
||||
if modified {
|
||||
_, err = c.addonClient.AddonV1alpha1().ManagedClusterAddOns(namespace).Update(ctx, addonCopy, metav1.UpdateOptions{})
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
94
pkg/addon/controllers/addonowner/controller_test.go
Normal file
94
pkg/addon/controllers/addonowner/controller_test.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package addonowner
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
clienttesting "k8s.io/client-go/testing"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/addonmanager/addontesting"
|
||||
"open-cluster-management.io/addon-framework/pkg/utils"
|
||||
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
fakeaddon "open-cluster-management.io/api/client/addon/clientset/versioned/fake"
|
||||
addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions"
|
||||
|
||||
testingcommon "open-cluster-management.io/ocm/pkg/common/testing"
|
||||
)
|
||||
|
||||
func newClusterManagementOwner(name string) metav1.OwnerReference {
|
||||
clusterManagementAddon := addontesting.NewClusterManagementAddon(name, "testcrd", "testcr").Build()
|
||||
return *metav1.NewControllerRef(clusterManagementAddon, addonapiv1alpha1.GroupVersion.WithKind("ClusterManagementAddOn"))
|
||||
}
|
||||
|
||||
func TestReconcile(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
syncKey string
|
||||
managedClusteraddon []runtime.Object
|
||||
clusterManagementAddon []runtime.Object
|
||||
validateAddonActions func(t *testing.T, actions []clienttesting.Action)
|
||||
}{
|
||||
{
|
||||
name: "no clustermanagementaddon",
|
||||
syncKey: "test/test",
|
||||
managedClusteraddon: []runtime.Object{},
|
||||
clusterManagementAddon: []runtime.Object{},
|
||||
validateAddonActions: addontesting.AssertNoActions,
|
||||
},
|
||||
{
|
||||
name: "no managedclusteraddon to sync",
|
||||
syncKey: "cluster1/test",
|
||||
managedClusteraddon: []runtime.Object{},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "testcrd", "testcr").Build()},
|
||||
validateAddonActions: addontesting.AssertNoActions,
|
||||
},
|
||||
{
|
||||
name: "update managedclusteraddon",
|
||||
syncKey: "cluster1/test",
|
||||
managedClusteraddon: []runtime.Object{
|
||||
addontesting.NewAddon("test", "cluster1", newClusterManagementOwner("test")),
|
||||
},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "testcrd", "testcr").Build()},
|
||||
validateAddonActions: addontesting.AssertNoActions,
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
obj := append(c.clusterManagementAddon, c.managedClusteraddon...)
|
||||
fakeAddonClient := fakeaddon.NewSimpleClientset(obj...)
|
||||
|
||||
addonInformers := addoninformers.NewSharedInformerFactory(fakeAddonClient, 10*time.Minute)
|
||||
|
||||
for _, obj := range c.managedClusteraddon {
|
||||
if err := addonInformers.Addon().V1alpha1().ManagedClusterAddOns().Informer().GetStore().Add(obj); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for _, obj := range c.clusterManagementAddon {
|
||||
if err := addonInformers.Addon().V1alpha1().ClusterManagementAddOns().Informer().GetStore().Add(obj); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
syncContext := testingcommon.NewFakeSyncContext(t, c.syncKey)
|
||||
recorder := syncContext.Recorder()
|
||||
|
||||
controller := NewAddonOwnerController(
|
||||
fakeAddonClient,
|
||||
addonInformers.Addon().V1alpha1().ManagedClusterAddOns(),
|
||||
addonInformers.Addon().V1alpha1().ClusterManagementAddOns(),
|
||||
utils.ManagedByAddonManager,
|
||||
recorder)
|
||||
|
||||
err := controller.Sync(context.TODO(), syncContext)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error when sync: %v", err)
|
||||
}
|
||||
c.validateAddonActions(t, fakeAddonClient.Actions())
|
||||
})
|
||||
}
|
||||
}
|
||||
344
pkg/addon/controllers/addonprogressing/controller.go
Normal file
344
pkg/addon/controllers/addonprogressing/controller.go
Normal file
@@ -0,0 +1,344 @@
|
||||
package addonprogressing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
jsonpatch "github.com/evanphx/json-patch"
|
||||
"github.com/openshift/library-go/pkg/controller/factory"
|
||||
"github.com/openshift/library-go/pkg/operator/events"
|
||||
"k8s.io/apimachinery/pkg/api/equality"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/selection"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
"k8s.io/klog/v2"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/addonmanager/constants"
|
||||
"open-cluster-management.io/addon-framework/pkg/addonmanager/controllers/agentdeploy"
|
||||
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
addonv1alpha1client "open-cluster-management.io/api/client/addon/clientset/versioned"
|
||||
addoninformerv1alpha1 "open-cluster-management.io/api/client/addon/informers/externalversions/addon/v1alpha1"
|
||||
addonlisterv1alpha1 "open-cluster-management.io/api/client/addon/listers/addon/v1alpha1"
|
||||
workinformers "open-cluster-management.io/api/client/work/informers/externalversions/work/v1"
|
||||
worklister "open-cluster-management.io/api/client/work/listers/work/v1"
|
||||
workapiv1 "open-cluster-management.io/api/work/v1"
|
||||
)
|
||||
|
||||
const (
|
||||
ProgressingDoing string = "Doing"
|
||||
ProgressingSucceed string = "Succeed"
|
||||
ProgressingFailed string = "Failed"
|
||||
)
|
||||
|
||||
// addonProgressingController reconciles instances of managedclusteradd on the hub
|
||||
// based to update the status progressing condition and last applied config
|
||||
type addonProgressingController struct {
|
||||
addonClient addonv1alpha1client.Interface
|
||||
managedClusterAddonLister addonlisterv1alpha1.ManagedClusterAddOnLister
|
||||
clusterManagementAddonLister addonlisterv1alpha1.ClusterManagementAddOnLister
|
||||
workLister worklister.ManifestWorkLister
|
||||
addonFilterFunc factory.EventFilterFunc
|
||||
}
|
||||
|
||||
func NewAddonProgressingController(
|
||||
addonClient addonv1alpha1client.Interface,
|
||||
addonInformers addoninformerv1alpha1.ManagedClusterAddOnInformer,
|
||||
clusterManagementAddonInformers addoninformerv1alpha1.ClusterManagementAddOnInformer,
|
||||
workInformers workinformers.ManifestWorkInformer,
|
||||
addonFilterFunc factory.EventFilterFunc,
|
||||
recorder events.Recorder,
|
||||
) factory.Controller {
|
||||
c := &addonProgressingController{
|
||||
addonClient: addonClient,
|
||||
managedClusterAddonLister: addonInformers.Lister(),
|
||||
clusterManagementAddonLister: clusterManagementAddonInformers.Lister(),
|
||||
workLister: workInformers.Lister(),
|
||||
addonFilterFunc: addonFilterFunc,
|
||||
}
|
||||
|
||||
return factory.New().WithInformersQueueKeysFunc(
|
||||
func(obj runtime.Object) []string {
|
||||
key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
|
||||
return []string{key}
|
||||
},
|
||||
addonInformers.Informer(), clusterManagementAddonInformers.Informer()).
|
||||
// TODO: consider hosted manifestwork
|
||||
WithInformersQueueKeysFunc(
|
||||
func(obj runtime.Object) []string {
|
||||
accessor, _ := meta.Accessor(obj)
|
||||
return []string{fmt.Sprintf("%s/%s", accessor.GetNamespace(), accessor.GetLabels()[addonapiv1alpha1.AddonLabelKey])}
|
||||
},
|
||||
workInformers.Informer()).
|
||||
WithSync(c.sync).ToController("addon-progressing-controller", recorder)
|
||||
}
|
||||
|
||||
func (c *addonProgressingController) sync(ctx context.Context, syncCtx factory.SyncContext) error {
|
||||
key := syncCtx.QueueKey()
|
||||
klog.V(4).Infof("Reconciling addon %q", key)
|
||||
|
||||
namespace, addonName, err := cache.SplitMetaNamespaceKey(key)
|
||||
if err != nil {
|
||||
// ignore addon whose key is invalid
|
||||
return nil
|
||||
}
|
||||
|
||||
addon, err := c.managedClusterAddonLister.ManagedClusterAddOns(namespace).Get(addonName)
|
||||
switch {
|
||||
case errors.IsNotFound(err):
|
||||
return nil
|
||||
case err != nil:
|
||||
return err
|
||||
}
|
||||
|
||||
clusterManagementAddon, err := c.clusterManagementAddonLister.Get(addonName)
|
||||
if errors.IsNotFound(err) {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !c.addonFilterFunc(clusterManagementAddon) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// update progressing condition and last applied config
|
||||
return c.updateAddonProgressingAndLastApplied(ctx, addon.DeepCopy(), addon)
|
||||
}
|
||||
|
||||
func (c *addonProgressingController) updateAddonProgressingAndLastApplied(ctx context.Context, newaddon, oldaddon *addonapiv1alpha1.ManagedClusterAddOn) error {
|
||||
// check config references
|
||||
if supported, config := isConfigurationSupported(newaddon); !supported {
|
||||
meta.SetStatusCondition(&newaddon.Status.Conditions, metav1.Condition{
|
||||
Type: addonapiv1alpha1.ManagedClusterAddOnConditionProgressing,
|
||||
Status: metav1.ConditionFalse,
|
||||
Reason: addonapiv1alpha1.ProgressingReasonConfigurationUnsupported,
|
||||
Message: fmt.Sprintf("Configuration with gvr %s/%s is not supported for this addon", config.Group, config.Resource),
|
||||
})
|
||||
return c.patchAddOnProgressingAndLastApplied(ctx, newaddon, oldaddon)
|
||||
}
|
||||
|
||||
// wait until addon has ManifestApplied condition
|
||||
if cond := meta.FindStatusCondition(newaddon.Status.Conditions, addonapiv1alpha1.ManagedClusterAddOnManifestApplied); cond == nil {
|
||||
meta.SetStatusCondition(&newaddon.Status.Conditions, metav1.Condition{
|
||||
Type: addonapiv1alpha1.ManagedClusterAddOnConditionProgressing,
|
||||
Status: metav1.ConditionFalse,
|
||||
Reason: "WaitingForManifestApplied",
|
||||
Message: "Waiting for ManagedClusterAddOn ManifestApplied condition",
|
||||
})
|
||||
return c.patchAddOnProgressingAndLastApplied(ctx, newaddon, oldaddon)
|
||||
}
|
||||
|
||||
// set upgrade flag
|
||||
isUpgrade := false
|
||||
for _, configReference := range newaddon.Status.ConfigReferences {
|
||||
if configReference.LastAppliedConfig != nil && configReference.LastAppliedConfig.SpecHash != "" {
|
||||
isUpgrade = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// get addon works
|
||||
// TODO: consider hosted manifestwork
|
||||
requirement, _ := labels.NewRequirement(addonapiv1alpha1.AddonLabelKey, selection.Equals, []string{newaddon.Name})
|
||||
selector := labels.NewSelector().Add(*requirement)
|
||||
addonWorks, err := c.workLister.ManifestWorks(newaddon.Namespace).List(selector)
|
||||
if err != nil {
|
||||
setAddOnProgressingAndLastApplied(isUpgrade, ProgressingFailed, err.Error(), newaddon)
|
||||
return c.patchAddOnProgressingAndLastApplied(ctx, newaddon, oldaddon)
|
||||
}
|
||||
|
||||
if len(addonWorks) == 0 {
|
||||
setAddOnProgressingAndLastApplied(isUpgrade, ProgressingDoing, "no addon works", newaddon)
|
||||
return c.patchAddOnProgressingAndLastApplied(ctx, newaddon, oldaddon)
|
||||
}
|
||||
|
||||
// check addon manifestworks
|
||||
for _, work := range addonWorks {
|
||||
// skip pre-delete manifestwork
|
||||
if strings.HasPrefix(work.Name, constants.PreDeleteHookWorkName(newaddon.Name)) {
|
||||
continue
|
||||
}
|
||||
|
||||
// check if work configs matches addon configs
|
||||
if !workConfigsMatchesAddon(work, newaddon) {
|
||||
setAddOnProgressingAndLastApplied(isUpgrade, ProgressingDoing, "configs mismatch", newaddon)
|
||||
return c.patchAddOnProgressingAndLastApplied(ctx, newaddon, oldaddon)
|
||||
}
|
||||
|
||||
// check if work is ready
|
||||
if !workIsReady(work) {
|
||||
setAddOnProgressingAndLastApplied(isUpgrade, ProgressingDoing, "work is not ready", newaddon)
|
||||
return c.patchAddOnProgressingAndLastApplied(ctx, newaddon, oldaddon)
|
||||
}
|
||||
}
|
||||
|
||||
// set lastAppliedConfig when all the work matches addon and are ready.
|
||||
setAddOnProgressingAndLastApplied(isUpgrade, ProgressingSucceed, "", newaddon)
|
||||
return c.patchAddOnProgressingAndLastApplied(ctx, newaddon, oldaddon)
|
||||
}
|
||||
|
||||
func (c *addonProgressingController) patchAddOnProgressingAndLastApplied(ctx context.Context, new, old *addonapiv1alpha1.ManagedClusterAddOn) error {
|
||||
if equality.Semantic.DeepEqual(new.Status, old.Status) {
|
||||
return nil
|
||||
}
|
||||
|
||||
oldData, err := json.Marshal(&addonapiv1alpha1.ManagedClusterAddOn{
|
||||
Status: addonapiv1alpha1.ManagedClusterAddOnStatus{
|
||||
ConfigReferences: old.Status.ConfigReferences,
|
||||
Conditions: old.Status.Conditions,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
newData, err := json.Marshal(&addonapiv1alpha1.ManagedClusterAddOn{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
UID: new.UID,
|
||||
ResourceVersion: new.ResourceVersion,
|
||||
},
|
||||
Status: addonapiv1alpha1.ManagedClusterAddOnStatus{
|
||||
ConfigReferences: new.Status.ConfigReferences,
|
||||
Conditions: new.Status.Conditions,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
patchBytes, err := jsonpatch.CreateMergePatch(oldData, newData)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create patch for addon %s: %w", new.Name, err)
|
||||
}
|
||||
|
||||
klog.V(2).Infof("Patching addon %s/%s condition and last applied config with %s", new.Namespace, new.Name, string(patchBytes))
|
||||
addon, err := c.addonClient.AddonV1alpha1().ManagedClusterAddOns(new.Namespace).Patch(
|
||||
ctx, new.Name, types.MergePatchType, patchBytes, metav1.PatchOptions{}, "status")
|
||||
fmt.Printf("%v", addon)
|
||||
return err
|
||||
}
|
||||
|
||||
func isConfigurationSupported(addon *addonapiv1alpha1.ManagedClusterAddOn) (bool, addonapiv1alpha1.ConfigGroupResource) {
|
||||
supportedConfigSet := map[addonapiv1alpha1.ConfigGroupResource]bool{}
|
||||
for _, config := range addon.Status.SupportedConfigs {
|
||||
supportedConfigSet[config] = true
|
||||
}
|
||||
|
||||
for _, config := range addon.Spec.Configs {
|
||||
if _, ok := supportedConfigSet[config.ConfigGroupResource]; !ok {
|
||||
return false, config.ConfigGroupResource
|
||||
}
|
||||
}
|
||||
|
||||
return true, addonapiv1alpha1.ConfigGroupResource{}
|
||||
}
|
||||
|
||||
func workConfigsMatchesAddon(work *workapiv1.ManifestWork, addon *addonapiv1alpha1.ManagedClusterAddOn) bool {
|
||||
// get work spec hash
|
||||
if _, ok := work.Annotations[workapiv1.ManifestConfigSpecHashAnnotationKey]; !ok {
|
||||
return len(addon.Status.ConfigReferences) == 0
|
||||
}
|
||||
|
||||
// parse work spec hash
|
||||
workSpecHashMap := make(map[string]string)
|
||||
if err := json.Unmarshal([]byte(work.Annotations[workapiv1.ManifestConfigSpecHashAnnotationKey]), &workSpecHashMap); err != nil {
|
||||
klog.Warningf("%v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
// check work spec hash, all the config should have spec hash
|
||||
for _, v := range workSpecHashMap {
|
||||
if v == "" {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// check addon desired config
|
||||
for _, configReference := range addon.Status.ConfigReferences {
|
||||
if configReference.DesiredConfig == nil || configReference.DesiredConfig.SpecHash == "" {
|
||||
return false
|
||||
}
|
||||
}
|
||||
addonSpecHashMap := agentdeploy.ConfigsToMap(addon.Status.ConfigReferences)
|
||||
|
||||
// compare work and addon configs
|
||||
return equality.Semantic.DeepEqual(workSpecHashMap, addonSpecHashMap)
|
||||
}
|
||||
|
||||
// work is ready when
|
||||
// 1) condition Available status is true.
|
||||
// 2) condition Available observedGeneration equals to generation.
|
||||
// 3) If it is a fresh install since one addon can have multiple ManifestWorks, the ManifestWork condition ManifestApplied must also be true.
|
||||
func workIsReady(work *workapiv1.ManifestWork) bool {
|
||||
cond := meta.FindStatusCondition(work.Status.Conditions, workapiv1.WorkAvailable)
|
||||
if cond == nil || cond.Status != metav1.ConditionTrue || cond.ObservedGeneration != work.Generation {
|
||||
return false
|
||||
}
|
||||
cond = meta.FindStatusCondition(work.Status.Conditions, workapiv1.WorkApplied)
|
||||
if cond == nil || cond.Status != metav1.ConditionTrue || cond.ObservedGeneration != work.Generation {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// set addon progressing condition and last applied
|
||||
func setAddOnProgressingAndLastApplied(isUpgrade bool, status string, message string, addon *addonapiv1alpha1.ManagedClusterAddOn) {
|
||||
// always update progressing condition when there is no config
|
||||
// skip update progressing condition when last applied config already the same as desired
|
||||
skip := len(addon.Status.ConfigReferences) > 0
|
||||
for _, configReference := range addon.Status.ConfigReferences {
|
||||
if !equality.Semantic.DeepEqual(configReference.LastAppliedConfig, configReference.DesiredConfig) {
|
||||
skip = false
|
||||
}
|
||||
}
|
||||
if skip {
|
||||
return
|
||||
}
|
||||
|
||||
condition := metav1.Condition{
|
||||
Type: addonapiv1alpha1.ManagedClusterAddOnConditionProgressing,
|
||||
}
|
||||
switch status {
|
||||
case ProgressingDoing:
|
||||
condition.Status = metav1.ConditionTrue
|
||||
if isUpgrade {
|
||||
condition.Reason = addonapiv1alpha1.ProgressingReasonUpgrading
|
||||
condition.Message = fmt.Sprintf("upgrading... %v", message)
|
||||
} else {
|
||||
condition.Reason = addonapiv1alpha1.ProgressingReasonInstalling
|
||||
condition.Message = fmt.Sprintf("installing... %v", message)
|
||||
}
|
||||
case ProgressingSucceed:
|
||||
condition.Status = metav1.ConditionFalse
|
||||
for i, configReference := range addon.Status.ConfigReferences {
|
||||
addon.Status.ConfigReferences[i].LastAppliedConfig = configReference.DesiredConfig.DeepCopy()
|
||||
}
|
||||
if isUpgrade {
|
||||
condition.Reason = addonapiv1alpha1.ProgressingReasonUpgradeSucceed
|
||||
condition.Message = "upgrade completed with no errors."
|
||||
} else {
|
||||
condition.Reason = addonapiv1alpha1.ProgressingReasonInstallSucceed
|
||||
condition.Message = "install completed with no errors."
|
||||
}
|
||||
case ProgressingFailed:
|
||||
condition.Status = metav1.ConditionFalse
|
||||
if isUpgrade {
|
||||
condition.Reason = addonapiv1alpha1.ProgressingReasonUpgradeFailed
|
||||
condition.Message = message
|
||||
} else {
|
||||
condition.Reason = addonapiv1alpha1.ProgressingReasonInstallFailed
|
||||
condition.Message = message
|
||||
}
|
||||
}
|
||||
meta.SetStatusCondition(&addon.Status.Conditions, condition)
|
||||
}
|
||||
619
pkg/addon/controllers/addonprogressing/controller_test.go
Normal file
619
pkg/addon/controllers/addonprogressing/controller_test.go
Normal file
@@ -0,0 +1,619 @@
|
||||
package addonprogressing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
clienttesting "k8s.io/client-go/testing"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/addonmanager/addontesting"
|
||||
"open-cluster-management.io/addon-framework/pkg/agent"
|
||||
"open-cluster-management.io/addon-framework/pkg/utils"
|
||||
"open-cluster-management.io/api/addon/v1alpha1"
|
||||
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
fakeaddon "open-cluster-management.io/api/client/addon/clientset/versioned/fake"
|
||||
addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions"
|
||||
fakework "open-cluster-management.io/api/client/work/clientset/versioned/fake"
|
||||
workinformers "open-cluster-management.io/api/client/work/informers/externalversions"
|
||||
workapiv1 "open-cluster-management.io/api/work/v1"
|
||||
|
||||
testingcommon "open-cluster-management.io/ocm/pkg/common/testing"
|
||||
)
|
||||
|
||||
func newClusterManagementOwner(name string) metav1.OwnerReference {
|
||||
clusterManagementAddon := addontesting.NewClusterManagementAddon(name, "testcrd", "testcr").Build()
|
||||
return *metav1.NewControllerRef(clusterManagementAddon, addonapiv1alpha1.GroupVersion.WithKind("ClusterManagementAddOn"))
|
||||
}
|
||||
|
||||
func TestReconcile(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
syncKey string
|
||||
managedClusteraddon []runtime.Object
|
||||
clusterManagementAddon []runtime.Object
|
||||
work []runtime.Object
|
||||
validateAddonActions func(t *testing.T, actions []clienttesting.Action)
|
||||
}{
|
||||
{
|
||||
name: "no clustermanagementaddon",
|
||||
syncKey: "test/test",
|
||||
clusterManagementAddon: []runtime.Object{},
|
||||
managedClusteraddon: []runtime.Object{},
|
||||
work: []runtime.Object{},
|
||||
validateAddonActions: addontesting.AssertNoActions,
|
||||
},
|
||||
{
|
||||
name: "no managedClusteraddon",
|
||||
syncKey: "test/test",
|
||||
managedClusteraddon: []runtime.Object{},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "testcrd", "testcr").Build()},
|
||||
work: []runtime.Object{},
|
||||
validateAddonActions: addontesting.AssertNoActions,
|
||||
},
|
||||
{
|
||||
name: "no work applied condition",
|
||||
syncKey: "test/test",
|
||||
managedClusteraddon: []runtime.Object{
|
||||
addontesting.NewAddon("test", "cluster1"),
|
||||
},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "testcrd", "testcr").Build()},
|
||||
work: []runtime.Object{},
|
||||
validateAddonActions: addontesting.AssertNoActions,
|
||||
},
|
||||
{
|
||||
name: "update managedclusteraddon to installing when no work",
|
||||
syncKey: "cluster1/test",
|
||||
managedClusteraddon: []runtime.Object{func() *addonapiv1alpha1.ManagedClusterAddOn {
|
||||
addon := addontesting.NewAddon("test", "cluster1")
|
||||
meta.SetStatusCondition(&addon.Status.Conditions, metav1.Condition{
|
||||
Type: addonapiv1alpha1.ManagedClusterAddOnManifestApplied,
|
||||
Status: metav1.ConditionTrue,
|
||||
Reason: addonapiv1alpha1.AddonManifestAppliedReasonManifestsApplied,
|
||||
Message: "manifests of addon are applied successfully",
|
||||
})
|
||||
return addon
|
||||
}()},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "testcrd", "testcr").Build()},
|
||||
work: []runtime.Object{},
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch")
|
||||
actual := actions[0].(clienttesting.PatchActionImpl).Patch
|
||||
|
||||
addOn := &addonapiv1alpha1.ManagedClusterAddOn{}
|
||||
err := json.Unmarshal(actual, addOn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
configCond := meta.FindStatusCondition(addOn.Status.Conditions, addonapiv1alpha1.ManagedClusterAddOnConditionProgressing)
|
||||
if !(configCond != nil && configCond.Reason == addonapiv1alpha1.ProgressingReasonInstalling && configCond.Status == metav1.ConditionTrue) {
|
||||
t.Errorf("Condition Progressing is incorrect")
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "update managedclusteraddon to installing when work config spec not match",
|
||||
syncKey: "cluster1/test",
|
||||
managedClusteraddon: []runtime.Object{func() *addonapiv1alpha1.ManagedClusterAddOn {
|
||||
addon := addontesting.NewAddon("test", "cluster1")
|
||||
addon.Status.ConfigReferences = []addonapiv1alpha1.ConfigReference{
|
||||
{
|
||||
ConfigGroupResource: v1alpha1.ConfigGroupResource{Group: "core", Resource: "foo"},
|
||||
DesiredConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test", Namespace: "open-cluster-management"},
|
||||
SpecHash: "hashnew",
|
||||
},
|
||||
LastAppliedConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test", Namespace: "open-cluster-management"},
|
||||
SpecHash: "",
|
||||
},
|
||||
},
|
||||
}
|
||||
meta.SetStatusCondition(&addon.Status.Conditions, metav1.Condition{
|
||||
Type: addonapiv1alpha1.ManagedClusterAddOnManifestApplied,
|
||||
Status: metav1.ConditionTrue,
|
||||
Reason: addonapiv1alpha1.AddonManifestAppliedReasonManifestsApplied,
|
||||
Message: "manifests of addon are applied successfully",
|
||||
})
|
||||
return addon
|
||||
}()},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "testcrd", "testcr").Build()},
|
||||
work: []runtime.Object{func() *workapiv1.ManifestWork {
|
||||
work := addontesting.NewManifestWork(
|
||||
"addon-test-deploy",
|
||||
"cluster1",
|
||||
addontesting.NewUnstructured("v1", "ConfigMap", "default", "test1"),
|
||||
addontesting.NewUnstructured("v1", "Deployment", "default", "test1"),
|
||||
)
|
||||
work.SetLabels(map[string]string{
|
||||
addonapiv1alpha1.AddonLabelKey: "test",
|
||||
})
|
||||
work.SetAnnotations(map[string]string{
|
||||
workapiv1.ManifestConfigSpecHashAnnotationKey: "{\"foo.core/open-cluster-management/test\":\"hash\"}",
|
||||
})
|
||||
work.Status.Conditions = []metav1.Condition{
|
||||
{
|
||||
Type: workapiv1.WorkApplied,
|
||||
Status: metav1.ConditionTrue,
|
||||
},
|
||||
{
|
||||
Type: workapiv1.WorkAvailable,
|
||||
Status: metav1.ConditionTrue,
|
||||
},
|
||||
}
|
||||
return work
|
||||
}()},
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch")
|
||||
actual := actions[0].(clienttesting.PatchActionImpl).Patch
|
||||
|
||||
addOn := &addonapiv1alpha1.ManagedClusterAddOn{}
|
||||
err := json.Unmarshal(actual, addOn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
configCond := meta.FindStatusCondition(addOn.Status.Conditions, addonapiv1alpha1.ManagedClusterAddOnConditionProgressing)
|
||||
if !(configCond != nil && configCond.Reason == addonapiv1alpha1.ProgressingReasonInstalling && configCond.Status == metav1.ConditionTrue) {
|
||||
t.Errorf("Condition Progressing is incorrect")
|
||||
}
|
||||
if len(addOn.Status.ConfigReferences) != 0 {
|
||||
t.Errorf("ConfigReferences object is not correct: %v", addOn.Status.ConfigReferences)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "update managedclusteraddon to installing when work is not ready",
|
||||
syncKey: "cluster1/test",
|
||||
managedClusteraddon: []runtime.Object{func() *addonapiv1alpha1.ManagedClusterAddOn {
|
||||
addon := addontesting.NewAddon("test", "cluster1")
|
||||
addon.Status.ConfigReferences = []addonapiv1alpha1.ConfigReference{
|
||||
{
|
||||
ConfigGroupResource: v1alpha1.ConfigGroupResource{Group: "core", Resource: "foo"},
|
||||
DesiredConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test", Namespace: "open-cluster-management"},
|
||||
SpecHash: "hashnew",
|
||||
},
|
||||
LastAppliedConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test", Namespace: "open-cluster-management"},
|
||||
SpecHash: "",
|
||||
},
|
||||
},
|
||||
}
|
||||
meta.SetStatusCondition(&addon.Status.Conditions, metav1.Condition{
|
||||
Type: addonapiv1alpha1.ManagedClusterAddOnManifestApplied,
|
||||
Status: metav1.ConditionTrue,
|
||||
Reason: addonapiv1alpha1.AddonManifestAppliedReasonManifestsApplied,
|
||||
Message: "manifests of addon are applied successfully",
|
||||
})
|
||||
return addon
|
||||
}()},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "testcrd", "testcr").Build()},
|
||||
work: []runtime.Object{func() *workapiv1.ManifestWork {
|
||||
work := addontesting.NewManifestWork(
|
||||
"addon-test-deploy",
|
||||
"cluster1",
|
||||
addontesting.NewUnstructured("v1", "ConfigMap", "default", "test1"),
|
||||
addontesting.NewUnstructured("v1", "Deployment", "default", "test1"),
|
||||
)
|
||||
work.SetLabels(map[string]string{
|
||||
addonapiv1alpha1.AddonLabelKey: "test",
|
||||
})
|
||||
work.SetAnnotations(map[string]string{
|
||||
workapiv1.ManifestConfigSpecHashAnnotationKey: "{\"foo.core/open-cluster-management/test\":\"hashnew\"}",
|
||||
})
|
||||
work.Status.Conditions = []metav1.Condition{
|
||||
{
|
||||
Type: workapiv1.WorkApplied,
|
||||
Status: metav1.ConditionFalse,
|
||||
},
|
||||
{
|
||||
Type: workapiv1.WorkAvailable,
|
||||
Status: metav1.ConditionTrue,
|
||||
},
|
||||
}
|
||||
return work
|
||||
}()},
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch")
|
||||
actual := actions[0].(clienttesting.PatchActionImpl).Patch
|
||||
|
||||
addOn := &addonapiv1alpha1.ManagedClusterAddOn{}
|
||||
err := json.Unmarshal(actual, addOn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
configCond := meta.FindStatusCondition(addOn.Status.Conditions, addonapiv1alpha1.ManagedClusterAddOnConditionProgressing)
|
||||
if !(configCond != nil && configCond.Reason == addonapiv1alpha1.ProgressingReasonInstalling && configCond.Status == metav1.ConditionTrue) {
|
||||
t.Errorf("Condition Progressing is incorrect")
|
||||
}
|
||||
if len(addOn.Status.ConfigReferences) != 0 {
|
||||
t.Errorf("ConfigReferences object is not correct: %v", addOn.Status.ConfigReferences)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "update managedclusteraddon to uprading when work config spec not match",
|
||||
syncKey: "cluster1/test",
|
||||
managedClusteraddon: []runtime.Object{func() *addonapiv1alpha1.ManagedClusterAddOn {
|
||||
addon := addontesting.NewAddon("test", "cluster1")
|
||||
addon.Status.ConfigReferences = []addonapiv1alpha1.ConfigReference{
|
||||
{
|
||||
ConfigGroupResource: v1alpha1.ConfigGroupResource{Group: "core", Resource: "foo"},
|
||||
DesiredConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test", Namespace: "open-cluster-management"},
|
||||
SpecHash: "hashnew",
|
||||
},
|
||||
LastAppliedConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test", Namespace: "open-cluster-management"},
|
||||
SpecHash: "hash",
|
||||
},
|
||||
},
|
||||
}
|
||||
meta.SetStatusCondition(&addon.Status.Conditions, metav1.Condition{
|
||||
Type: addonapiv1alpha1.ManagedClusterAddOnManifestApplied,
|
||||
Status: metav1.ConditionTrue,
|
||||
Reason: addonapiv1alpha1.AddonManifestAppliedReasonManifestsApplied,
|
||||
Message: "manifests of addon are applied successfully",
|
||||
})
|
||||
return addon
|
||||
}()},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "testcrd", "testcr").Build()},
|
||||
work: []runtime.Object{func() *workapiv1.ManifestWork {
|
||||
work := addontesting.NewManifestWork(
|
||||
"addon-test-deploy",
|
||||
"cluster1",
|
||||
addontesting.NewUnstructured("v1", "ConfigMap", "default", "test1"),
|
||||
addontesting.NewUnstructured("v1", "Deployment", "default", "test1"),
|
||||
)
|
||||
work.SetLabels(map[string]string{
|
||||
addonapiv1alpha1.AddonLabelKey: "test",
|
||||
})
|
||||
work.SetAnnotations(map[string]string{
|
||||
workapiv1.ManifestConfigSpecHashAnnotationKey: "{\"foo.core/open-cluster-management/test\":\"hash\"}",
|
||||
})
|
||||
work.Status.Conditions = []metav1.Condition{
|
||||
{
|
||||
Type: workapiv1.WorkApplied,
|
||||
Status: metav1.ConditionTrue,
|
||||
},
|
||||
{
|
||||
Type: workapiv1.WorkAvailable,
|
||||
Status: metav1.ConditionTrue,
|
||||
},
|
||||
}
|
||||
return work
|
||||
}()},
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch")
|
||||
actual := actions[0].(clienttesting.PatchActionImpl).Patch
|
||||
|
||||
addOn := &addonapiv1alpha1.ManagedClusterAddOn{}
|
||||
err := json.Unmarshal(actual, addOn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
configCond := meta.FindStatusCondition(addOn.Status.Conditions, addonapiv1alpha1.ManagedClusterAddOnConditionProgressing)
|
||||
if !(configCond != nil && configCond.Reason == addonapiv1alpha1.ProgressingReasonUpgrading && configCond.Status == metav1.ConditionTrue) {
|
||||
t.Errorf("Condition Progressing is incorrect")
|
||||
}
|
||||
if len(addOn.Status.ConfigReferences) != 0 {
|
||||
t.Errorf("ConfigReferences object is not correct: %v", addOn.Status.ConfigReferences)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "update managedclusteraddon to uprading when work is not ready",
|
||||
syncKey: "cluster1/test",
|
||||
managedClusteraddon: []runtime.Object{func() *addonapiv1alpha1.ManagedClusterAddOn {
|
||||
addon := addontesting.NewAddon("test", "cluster1")
|
||||
addon.Status.ConfigReferences = []addonapiv1alpha1.ConfigReference{
|
||||
{
|
||||
ConfigGroupResource: v1alpha1.ConfigGroupResource{Group: "core", Resource: "foo"},
|
||||
DesiredConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test", Namespace: "open-cluster-management"},
|
||||
SpecHash: "hashnew",
|
||||
},
|
||||
LastAppliedConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test", Namespace: "open-cluster-management"},
|
||||
SpecHash: "hash",
|
||||
},
|
||||
},
|
||||
}
|
||||
meta.SetStatusCondition(&addon.Status.Conditions, metav1.Condition{
|
||||
Type: addonapiv1alpha1.ManagedClusterAddOnManifestApplied,
|
||||
Status: metav1.ConditionTrue,
|
||||
Reason: addonapiv1alpha1.AddonManifestAppliedReasonManifestsApplied,
|
||||
Message: "manifests of addon are applied successfully",
|
||||
})
|
||||
return addon
|
||||
}()},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "testcrd", "testcr").Build()},
|
||||
work: []runtime.Object{func() *workapiv1.ManifestWork {
|
||||
work := addontesting.NewManifestWork(
|
||||
"addon-test-deploy",
|
||||
"cluster1",
|
||||
addontesting.NewUnstructured("v1", "ConfigMap", "default", "test1"),
|
||||
addontesting.NewUnstructured("v1", "Deployment", "default", "test1"),
|
||||
)
|
||||
work.SetLabels(map[string]string{
|
||||
addonapiv1alpha1.AddonLabelKey: "test",
|
||||
})
|
||||
work.SetAnnotations(map[string]string{
|
||||
workapiv1.ManifestConfigSpecHashAnnotationKey: "{\"foo.core/open-cluster-management/test\":\"hashnew\"}",
|
||||
})
|
||||
work.Status.Conditions = []metav1.Condition{
|
||||
{
|
||||
Type: workapiv1.WorkApplied,
|
||||
Status: metav1.ConditionTrue,
|
||||
},
|
||||
{
|
||||
Type: workapiv1.WorkAvailable,
|
||||
Status: metav1.ConditionFalse,
|
||||
},
|
||||
}
|
||||
return work
|
||||
}()},
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch")
|
||||
actual := actions[0].(clienttesting.PatchActionImpl).Patch
|
||||
|
||||
addOn := &addonapiv1alpha1.ManagedClusterAddOn{}
|
||||
err := json.Unmarshal(actual, addOn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
configCond := meta.FindStatusCondition(addOn.Status.Conditions, addonapiv1alpha1.ManagedClusterAddOnConditionProgressing)
|
||||
if !(configCond != nil && configCond.Reason == addonapiv1alpha1.ProgressingReasonUpgrading && configCond.Status == metav1.ConditionTrue) {
|
||||
t.Errorf("Condition Progressing is incorrect")
|
||||
}
|
||||
if len(addOn.Status.ConfigReferences) != 0 {
|
||||
t.Errorf("ConfigReferences object is not correct: %v", addOn.Status.ConfigReferences)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "update managedclusteraddon to install succeed",
|
||||
syncKey: "cluster1/test",
|
||||
managedClusteraddon: []runtime.Object{func() *addonapiv1alpha1.ManagedClusterAddOn {
|
||||
addon := addontesting.NewAddon("test", "cluster1")
|
||||
addon.Status.ConfigReferences = []addonapiv1alpha1.ConfigReference{
|
||||
{
|
||||
ConfigGroupResource: v1alpha1.ConfigGroupResource{Group: "core", Resource: "foo"},
|
||||
DesiredConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test", Namespace: "open-cluster-management"},
|
||||
SpecHash: "hashnew",
|
||||
},
|
||||
LastAppliedConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test", Namespace: "open-cluster-management"},
|
||||
SpecHash: "",
|
||||
},
|
||||
},
|
||||
}
|
||||
meta.SetStatusCondition(&addon.Status.Conditions, metav1.Condition{
|
||||
Type: addonapiv1alpha1.ManagedClusterAddOnManifestApplied,
|
||||
Status: metav1.ConditionTrue,
|
||||
Reason: addonapiv1alpha1.AddonManifestAppliedReasonManifestsApplied,
|
||||
Message: "manifests of addon are applied successfully",
|
||||
})
|
||||
return addon
|
||||
}()},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "testcrd", "testcr").Build()},
|
||||
work: []runtime.Object{func() *workapiv1.ManifestWork {
|
||||
work := addontesting.NewManifestWork(
|
||||
"addon-test-deploy",
|
||||
"cluster1",
|
||||
addontesting.NewUnstructured("v1", "ConfigMap", "default", "test1"),
|
||||
addontesting.NewUnstructured("v1", "Deployment", "default", "test1"),
|
||||
)
|
||||
work.SetLabels(map[string]string{
|
||||
addonapiv1alpha1.AddonLabelKey: "test",
|
||||
})
|
||||
work.SetAnnotations(map[string]string{
|
||||
workapiv1.ManifestConfigSpecHashAnnotationKey: "{\"foo.core/open-cluster-management/test\":\"hashnew\"}",
|
||||
})
|
||||
work.Status.Conditions = []metav1.Condition{
|
||||
{
|
||||
Type: workapiv1.WorkApplied,
|
||||
Status: metav1.ConditionTrue,
|
||||
},
|
||||
{
|
||||
Type: workapiv1.WorkAvailable,
|
||||
Status: metav1.ConditionTrue,
|
||||
},
|
||||
}
|
||||
return work
|
||||
}()},
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch")
|
||||
actual := actions[0].(clienttesting.PatchActionImpl).Patch
|
||||
|
||||
addOn := &addonapiv1alpha1.ManagedClusterAddOn{}
|
||||
err := json.Unmarshal(actual, addOn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
configCond := meta.FindStatusCondition(addOn.Status.Conditions, addonapiv1alpha1.ManagedClusterAddOnConditionProgressing)
|
||||
if !(configCond != nil && configCond.Reason == addonapiv1alpha1.ProgressingReasonInstallSucceed && configCond.Status == metav1.ConditionFalse) {
|
||||
t.Errorf("Condition Progressing is incorrect")
|
||||
}
|
||||
if len(addOn.Status.ConfigReferences) != 1 {
|
||||
t.Errorf("ConfigReferences object is not correct: %v", addOn.Status.ConfigReferences)
|
||||
}
|
||||
if addOn.Status.ConfigReferences[0].LastAppliedConfig.SpecHash != addOn.Status.ConfigReferences[0].DesiredConfig.SpecHash {
|
||||
t.Errorf("LastAppliedConfig object is not correct: %v", addOn.Status.ConfigReferences[0].LastAppliedConfig.SpecHash)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "update managedclusteraddon to upgrade succeed",
|
||||
syncKey: "cluster1/test",
|
||||
managedClusteraddon: []runtime.Object{func() *addonapiv1alpha1.ManagedClusterAddOn {
|
||||
addon := addontesting.NewAddon("test", "cluster1")
|
||||
addon.Status.ConfigReferences = []addonapiv1alpha1.ConfigReference{
|
||||
{
|
||||
ConfigGroupResource: v1alpha1.ConfigGroupResource{Group: "core", Resource: "foo"},
|
||||
DesiredConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test", Namespace: "open-cluster-management"},
|
||||
SpecHash: "hashnew",
|
||||
},
|
||||
LastAppliedConfig: &v1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v1alpha1.ConfigReferent{Name: "test", Namespace: "open-cluster-management"},
|
||||
SpecHash: "hash",
|
||||
},
|
||||
},
|
||||
}
|
||||
meta.SetStatusCondition(&addon.Status.Conditions, metav1.Condition{
|
||||
Type: addonapiv1alpha1.ManagedClusterAddOnManifestApplied,
|
||||
Status: metav1.ConditionTrue,
|
||||
Reason: addonapiv1alpha1.AddonManifestAppliedReasonManifestsApplied,
|
||||
Message: "manifests of addon are applied successfully",
|
||||
})
|
||||
return addon
|
||||
}()},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "testcrd", "testcr").Build()},
|
||||
work: []runtime.Object{func() *workapiv1.ManifestWork {
|
||||
work := addontesting.NewManifestWork(
|
||||
"addon-test-deploy",
|
||||
"cluster1",
|
||||
addontesting.NewUnstructured("v1", "ConfigMap", "default", "test1"),
|
||||
addontesting.NewUnstructured("v1", "Deployment", "default", "test1"),
|
||||
)
|
||||
work.SetLabels(map[string]string{
|
||||
addonapiv1alpha1.AddonLabelKey: "test",
|
||||
})
|
||||
work.SetAnnotations(map[string]string{
|
||||
workapiv1.ManifestConfigSpecHashAnnotationKey: "{\"foo.core/open-cluster-management/test\":\"hashnew\"}",
|
||||
})
|
||||
work.Status.Conditions = []metav1.Condition{
|
||||
{
|
||||
Type: workapiv1.WorkApplied,
|
||||
Status: metav1.ConditionTrue,
|
||||
},
|
||||
{
|
||||
Type: workapiv1.WorkAvailable,
|
||||
Status: metav1.ConditionTrue,
|
||||
},
|
||||
}
|
||||
return work
|
||||
}()},
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch")
|
||||
actual := actions[0].(clienttesting.PatchActionImpl).Patch
|
||||
|
||||
addOn := &addonapiv1alpha1.ManagedClusterAddOn{}
|
||||
err := json.Unmarshal(actual, addOn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
configCond := meta.FindStatusCondition(addOn.Status.Conditions, addonapiv1alpha1.ManagedClusterAddOnConditionProgressing)
|
||||
if !(configCond != nil && configCond.Reason == addonapiv1alpha1.ProgressingReasonUpgradeSucceed && configCond.Status == metav1.ConditionFalse) {
|
||||
t.Errorf("Condition Progressing is incorrect")
|
||||
}
|
||||
if len(addOn.Status.ConfigReferences) != 1 {
|
||||
t.Errorf("ConfigReferences object is not correct: %v", addOn.Status.ConfigReferences)
|
||||
}
|
||||
if addOn.Status.ConfigReferences[0].LastAppliedConfig.SpecHash != addOn.Status.ConfigReferences[0].DesiredConfig.SpecHash {
|
||||
t.Errorf("LastAppliedConfig object is not correct: %v", addOn.Status.ConfigReferences[0].LastAppliedConfig.SpecHash)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "update managedclusteraddon to configuration unsupported...",
|
||||
syncKey: "cluster1/test",
|
||||
managedClusteraddon: []runtime.Object{
|
||||
func() *addonapiv1alpha1.ManagedClusterAddOn {
|
||||
addon := addontesting.NewAddon("test", "cluster1")
|
||||
addon.Spec.Configs = []addonapiv1alpha1.AddOnConfig{
|
||||
{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: "config1.test",
|
||||
Resource: "config1",
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: "cluster1",
|
||||
Name: "override",
|
||||
},
|
||||
},
|
||||
}
|
||||
addon.Status.SupportedConfigs = []addonapiv1alpha1.ConfigGroupResource{
|
||||
{
|
||||
Group: "configs.test",
|
||||
Resource: "testconfigs",
|
||||
},
|
||||
}
|
||||
meta.SetStatusCondition(&addon.Status.Conditions, metav1.Condition{
|
||||
Type: addonapiv1alpha1.ManagedClusterAddOnManifestApplied,
|
||||
Status: metav1.ConditionTrue,
|
||||
Reason: addonapiv1alpha1.AddonManifestAppliedReasonManifestsApplied,
|
||||
Message: "manifests of addon are applied successfully",
|
||||
})
|
||||
return addon
|
||||
}(),
|
||||
},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "testcrd", "testcr").Build()},
|
||||
work: []runtime.Object{},
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch")
|
||||
patch := actions[0].(clienttesting.PatchAction).GetPatch()
|
||||
addOn := &addonapiv1alpha1.ManagedClusterAddOn{}
|
||||
err := json.Unmarshal(patch, addOn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
configCond := meta.FindStatusCondition(addOn.Status.Conditions, addonapiv1alpha1.ManagedClusterAddOnConditionProgressing)
|
||||
if !(configCond != nil && configCond.Reason == addonapiv1alpha1.ProgressingReasonConfigurationUnsupported && configCond.Status == metav1.ConditionFalse) {
|
||||
t.Errorf("Condition Progressing is incorrect")
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
fakeAddonClient := fakeaddon.NewSimpleClientset(c.managedClusteraddon...)
|
||||
fakeWorkClient := fakework.NewSimpleClientset()
|
||||
|
||||
addonInformers := addoninformers.NewSharedInformerFactory(fakeAddonClient, 10*time.Minute)
|
||||
workInformers := workinformers.NewSharedInformerFactory(fakeWorkClient, 10*time.Minute)
|
||||
|
||||
for _, obj := range c.managedClusteraddon {
|
||||
if err := addonInformers.Addon().V1alpha1().ManagedClusterAddOns().Informer().GetStore().Add(obj); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for _, obj := range c.clusterManagementAddon {
|
||||
if err := addonInformers.Addon().V1alpha1().ClusterManagementAddOns().Informer().GetStore().Add(obj); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for _, obj := range c.work {
|
||||
if err := workInformers.Work().V1().ManifestWorks().Informer().GetStore().Add(obj); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
syncContext := testingcommon.NewFakeSyncContext(t, c.syncKey)
|
||||
recorder := syncContext.Recorder()
|
||||
|
||||
controller := NewAddonProgressingController(
|
||||
fakeAddonClient,
|
||||
addonInformers.Addon().V1alpha1().ManagedClusterAddOns(),
|
||||
addonInformers.Addon().V1alpha1().ClusterManagementAddOns(),
|
||||
workInformers.Work().V1().ManifestWorks(),
|
||||
utils.ManagedBySelf(map[string]agent.AgentAddon{"test": nil}),
|
||||
recorder,
|
||||
)
|
||||
|
||||
err := controller.Sync(context.TODO(), syncContext)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error when sync: %v", err)
|
||||
}
|
||||
c.validateAddonActions(t, fakeAddonClient.Actions())
|
||||
})
|
||||
}
|
||||
}
|
||||
211
pkg/addon/controllers/addontemplate/controller.go
Normal file
211
pkg/addon/controllers/addontemplate/controller.go
Normal file
@@ -0,0 +1,211 @@
|
||||
package addontemplate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/openshift/library-go/pkg/controller/factory"
|
||||
"github.com/openshift/library-go/pkg/operator/events"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
utilrand "k8s.io/apimachinery/pkg/util/rand"
|
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
"k8s.io/client-go/dynamic/dynamicinformer"
|
||||
kubeinformers "k8s.io/client-go/informers"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
"k8s.io/klog/v2"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/addonfactory"
|
||||
"open-cluster-management.io/addon-framework/pkg/addonmanager"
|
||||
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
addonv1alpha1client "open-cluster-management.io/api/client/addon/clientset/versioned"
|
||||
addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions"
|
||||
addonlisterv1alpha1 "open-cluster-management.io/api/client/addon/listers/addon/v1alpha1"
|
||||
clusterv1informers "open-cluster-management.io/api/client/cluster/informers/externalversions"
|
||||
workv1informers "open-cluster-management.io/api/client/work/informers/externalversions"
|
||||
|
||||
"open-cluster-management.io/ocm/pkg/addon/templateagent"
|
||||
)
|
||||
|
||||
// addonTemplateController monitors ManagedClusterAddOns on hub to get all the in-used addon templates,
|
||||
// and starts an addon manager for every addon template to handle agent requests deployed by this template
|
||||
type addonTemplateController struct {
|
||||
// addonManagers holds all addon managers that will be deployed with template type addons.
|
||||
// The key is the name of the template type addon.
|
||||
addonManagers map[string]context.CancelFunc
|
||||
|
||||
kubeConfig *rest.Config
|
||||
addonClient addonv1alpha1client.Interface
|
||||
kubeClient kubernetes.Interface
|
||||
cmaLister addonlisterv1alpha1.ClusterManagementAddOnLister
|
||||
addonInformers addoninformers.SharedInformerFactory
|
||||
clusterInformers clusterv1informers.SharedInformerFactory
|
||||
dynamicInformers dynamicinformer.DynamicSharedInformerFactory
|
||||
workInformers workv1informers.SharedInformerFactory
|
||||
runControllerFunc runController
|
||||
}
|
||||
|
||||
type runController func(ctx context.Context, addonName string) error
|
||||
|
||||
// NewAddonTemplateController returns an instance of addonTemplateController
|
||||
func NewAddonTemplateController(
|
||||
hubKubeconfig *rest.Config,
|
||||
hubKubeClient kubernetes.Interface,
|
||||
addonClient addonv1alpha1client.Interface,
|
||||
addonInformers addoninformers.SharedInformerFactory,
|
||||
clusterInformers clusterv1informers.SharedInformerFactory,
|
||||
dynamicInformers dynamicinformer.DynamicSharedInformerFactory,
|
||||
workInformers workv1informers.SharedInformerFactory,
|
||||
recorder events.Recorder,
|
||||
runController ...runController,
|
||||
) factory.Controller {
|
||||
c := &addonTemplateController{
|
||||
kubeConfig: hubKubeconfig,
|
||||
kubeClient: hubKubeClient,
|
||||
addonClient: addonClient,
|
||||
cmaLister: addonInformers.Addon().V1alpha1().ClusterManagementAddOns().Lister(),
|
||||
addonManagers: make(map[string]context.CancelFunc),
|
||||
addonInformers: addonInformers,
|
||||
clusterInformers: clusterInformers,
|
||||
dynamicInformers: dynamicInformers,
|
||||
workInformers: workInformers,
|
||||
}
|
||||
|
||||
if len(runController) > 0 {
|
||||
c.runControllerFunc = runController[0]
|
||||
} else {
|
||||
// easy to mock in unit tests
|
||||
c.runControllerFunc = c.runController
|
||||
}
|
||||
return factory.New().WithInformersQueueKeysFunc(
|
||||
func(obj runtime.Object) []string {
|
||||
key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
|
||||
return []string{key}
|
||||
},
|
||||
addonInformers.Addon().V1alpha1().ClusterManagementAddOns().Informer()).
|
||||
WithSync(c.sync).
|
||||
ToController("addon-template-controller", recorder)
|
||||
}
|
||||
|
||||
func (c *addonTemplateController) stopUnusedManagers(
|
||||
ctx context.Context, syncCtx factory.SyncContext, addOnName string) {
|
||||
|
||||
stopFunc, ok := c.addonManagers[addOnName]
|
||||
if ok {
|
||||
stopFunc()
|
||||
klog.Infof("Stop the manager for addon %s", addOnName)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *addonTemplateController) sync(ctx context.Context, syncCtx factory.SyncContext) error {
|
||||
key := syncCtx.QueueKey()
|
||||
_, addonName, err := cache.SplitMetaNamespaceKey(key)
|
||||
if err != nil {
|
||||
// ignore addon whose key is not in format: namespace/name
|
||||
return nil
|
||||
}
|
||||
|
||||
cma, err := c.cmaLister.Get(addonName)
|
||||
if err != nil {
|
||||
if errors.IsNotFound(err) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if !templateagent.SupportAddOnTemplate(cma) {
|
||||
c.stopUnusedManagers(ctx, syncCtx, cma.Name)
|
||||
return nil
|
||||
}
|
||||
|
||||
_, exist := c.addonManagers[addonName]
|
||||
if exist {
|
||||
klog.Infof("There already is a manager started for addon %s, skip.", addonName)
|
||||
return nil
|
||||
}
|
||||
|
||||
klog.Infof("Start an addon manager for addon %s", addonName)
|
||||
|
||||
stopFunc := c.startManager(ctx, addonName)
|
||||
c.addonManagers[addonName] = stopFunc
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *addonTemplateController) startManager(
|
||||
pctx context.Context,
|
||||
addonName string) context.CancelFunc {
|
||||
ctx, stopFunc := context.WithCancel(pctx)
|
||||
go func() {
|
||||
err := c.runControllerFunc(ctx, addonName)
|
||||
if err != nil {
|
||||
klog.Errorf("run controller for addon %s error: %v", addonName, err)
|
||||
utilruntime.HandleError(err)
|
||||
}
|
||||
|
||||
// use the parent context to start all shared informers, otherwise once the context is cancelled,
|
||||
// the informers will stop and all other shared go routines will be impacted.
|
||||
c.workInformers.Start(pctx.Done())
|
||||
c.addonInformers.Start(pctx.Done())
|
||||
c.clusterInformers.Start(pctx.Done())
|
||||
c.dynamicInformers.Start(pctx.Done())
|
||||
|
||||
<-ctx.Done()
|
||||
klog.Infof("Addon %s Manager stopped", addonName)
|
||||
}()
|
||||
return stopFunc
|
||||
}
|
||||
|
||||
func (c *addonTemplateController) runController(
|
||||
ctx context.Context, addonName string) error {
|
||||
mgr, err := addonmanager.New(c.kubeConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
kubeInformers := kubeinformers.NewSharedInformerFactoryWithOptions(c.kubeClient, 10*time.Minute,
|
||||
kubeinformers.WithTweakListOptions(func(listOptions *metav1.ListOptions) {
|
||||
selector := &metav1.LabelSelector{
|
||||
MatchExpressions: []metav1.LabelSelectorRequirement{
|
||||
{
|
||||
Key: addonv1alpha1.AddonLabelKey,
|
||||
Operator: metav1.LabelSelectorOpIn,
|
||||
Values: []string{addonName},
|
||||
},
|
||||
},
|
||||
}
|
||||
listOptions.LabelSelector = metav1.FormatLabelSelector(selector)
|
||||
}),
|
||||
)
|
||||
|
||||
agentAddon := templateagent.NewCRDTemplateAgentAddon(
|
||||
addonName,
|
||||
// TODO: agentName should not be changed after restarting the agent
|
||||
utilrand.String(5),
|
||||
c.kubeClient,
|
||||
c.addonClient,
|
||||
c.addonInformers,
|
||||
kubeInformers.Rbac().V1().RoleBindings().Lister(),
|
||||
addonfactory.GetAddOnDeploymentConfigValues(
|
||||
addonfactory.NewAddOnDeploymentConfigGetter(c.addonClient),
|
||||
addonfactory.ToAddOnCustomizedVariableValues,
|
||||
templateagent.ToAddOnNodePlacementPrivateValues,
|
||||
templateagent.ToAddOnRegistriesPrivateValues,
|
||||
),
|
||||
)
|
||||
err = mgr.AddAgent(agentAddon)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = mgr.StartWithInformers(ctx, kubeInformers, c.workInformers, c.addonInformers, c.clusterInformers, c.dynamicInformers)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
kubeInformers.Start(ctx.Done())
|
||||
|
||||
return nil
|
||||
}
|
||||
217
pkg/addon/controllers/addontemplate/controller_test.go
Normal file
217
pkg/addon/controllers/addontemplate/controller_test.go
Normal file
@@ -0,0 +1,217 @@
|
||||
package addontemplate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/openshift/library-go/pkg/operator/events/eventstesting"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/client-go/dynamic/dynamicinformer"
|
||||
dynamicfake "k8s.io/client-go/dynamic/fake"
|
||||
fakekube "k8s.io/client-go/kubernetes/fake"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/addonmanager/addontesting"
|
||||
"open-cluster-management.io/addon-framework/pkg/utils"
|
||||
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
fakeaddon "open-cluster-management.io/api/client/addon/clientset/versioned/fake"
|
||||
addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions"
|
||||
fakecluster "open-cluster-management.io/api/client/cluster/clientset/versioned/fake"
|
||||
clusterv1informers "open-cluster-management.io/api/client/cluster/informers/externalversions"
|
||||
fakework "open-cluster-management.io/api/client/work/clientset/versioned/fake"
|
||||
workinformers "open-cluster-management.io/api/client/work/informers/externalversions"
|
||||
|
||||
testingcommon "open-cluster-management.io/ocm/pkg/common/testing"
|
||||
)
|
||||
|
||||
func TestReconcile(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
syncKeys []string
|
||||
managedClusteraddon []runtime.Object
|
||||
clusterManagementAddon []runtime.Object
|
||||
expectedCount int
|
||||
expectedTimeout bool
|
||||
}{
|
||||
{
|
||||
name: "no clustermanagementaddon",
|
||||
syncKeys: []string{"test"},
|
||||
managedClusteraddon: []runtime.Object{},
|
||||
clusterManagementAddon: []runtime.Object{},
|
||||
expectedCount: 0,
|
||||
expectedTimeout: true,
|
||||
},
|
||||
{
|
||||
name: "not template type clustermanagementaddon",
|
||||
syncKeys: []string{"test"},
|
||||
managedClusteraddon: []runtime.Object{},
|
||||
clusterManagementAddon: []runtime.Object{
|
||||
addontesting.NewClusterManagementAddon("test", "", "").Build()},
|
||||
expectedCount: 0,
|
||||
expectedTimeout: true,
|
||||
},
|
||||
{
|
||||
name: "one template type clustermanagementaddon",
|
||||
syncKeys: []string{"test"},
|
||||
managedClusteraddon: []runtime.Object{},
|
||||
clusterManagementAddon: []runtime.Object{
|
||||
addontesting.NewClusterManagementAddon("test", "", "").WithSupportedConfigs(
|
||||
addonv1alpha1.ConfigMeta{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{
|
||||
Group: utils.AddOnTemplateGVR.Group,
|
||||
Resource: utils.AddOnTemplateGVR.Resource,
|
||||
},
|
||||
DefaultConfig: &addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
}).Build()},
|
||||
expectedCount: 1,
|
||||
expectedTimeout: false,
|
||||
},
|
||||
{
|
||||
name: "two template type clustermanagementaddon",
|
||||
syncKeys: []string{"test", "test1"},
|
||||
managedClusteraddon: []runtime.Object{},
|
||||
clusterManagementAddon: []runtime.Object{
|
||||
addontesting.NewClusterManagementAddon("test", "", "").WithSupportedConfigs(
|
||||
addonv1alpha1.ConfigMeta{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{
|
||||
Group: utils.AddOnTemplateGVR.Group,
|
||||
Resource: utils.AddOnTemplateGVR.Resource,
|
||||
},
|
||||
DefaultConfig: &addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
}).Build(),
|
||||
addontesting.NewClusterManagementAddon("test1", "", "").WithSupportedConfigs(
|
||||
addonv1alpha1.ConfigMeta{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{
|
||||
Group: utils.AddOnTemplateGVR.Group,
|
||||
Resource: utils.AddOnTemplateGVR.Resource,
|
||||
},
|
||||
DefaultConfig: &addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
}).Build(),
|
||||
},
|
||||
expectedCount: 2,
|
||||
expectedTimeout: false,
|
||||
},
|
||||
{
|
||||
name: "two template type and one not template type clustermanagementaddon",
|
||||
syncKeys: []string{"test", "test1", "test2"},
|
||||
managedClusteraddon: []runtime.Object{},
|
||||
clusterManagementAddon: []runtime.Object{
|
||||
addontesting.NewClusterManagementAddon("test", "", "").WithSupportedConfigs(
|
||||
addonv1alpha1.ConfigMeta{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{
|
||||
Group: utils.AddOnTemplateGVR.Group,
|
||||
Resource: utils.AddOnTemplateGVR.Resource,
|
||||
},
|
||||
DefaultConfig: &addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
}).Build(),
|
||||
addontesting.NewClusterManagementAddon("test1", "", "").WithSupportedConfigs(
|
||||
addonv1alpha1.ConfigMeta{
|
||||
ConfigGroupResource: addonv1alpha1.ConfigGroupResource{
|
||||
Group: utils.AddOnTemplateGVR.Group,
|
||||
Resource: utils.AddOnTemplateGVR.Resource,
|
||||
},
|
||||
DefaultConfig: &addonv1alpha1.ConfigReferent{Name: "test"},
|
||||
}).Build(),
|
||||
addontesting.NewClusterManagementAddon("test2", "", "").Build(),
|
||||
},
|
||||
expectedCount: 2,
|
||||
expectedTimeout: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
count := 0
|
||||
var wg sync.WaitGroup
|
||||
lock := &sync.Mutex{}
|
||||
rederCount := func() int {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
return count
|
||||
}
|
||||
increaseCount := func() {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
count = count + 1
|
||||
}
|
||||
|
||||
for range c.syncKeys {
|
||||
wg.Add(1)
|
||||
}
|
||||
runController := func(ctx context.Context, addonName string) error {
|
||||
defer wg.Done()
|
||||
increaseCount()
|
||||
return nil
|
||||
}
|
||||
obj := append(c.clusterManagementAddon, c.managedClusteraddon...)
|
||||
fakeAddonClient := fakeaddon.NewSimpleClientset(obj...)
|
||||
|
||||
addonInformers := addoninformers.NewSharedInformerFactory(fakeAddonClient, 10*time.Minute)
|
||||
|
||||
for _, obj := range c.managedClusteraddon {
|
||||
if err := addonInformers.Addon().V1alpha1().ManagedClusterAddOns().Informer().GetStore().Add(obj); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for _, obj := range c.clusterManagementAddon {
|
||||
if err := addonInformers.Addon().V1alpha1().ClusterManagementAddOns().Informer().GetStore().Add(obj); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
fakeDynamicClient := dynamicfake.NewSimpleDynamicClient(runtime.NewScheme())
|
||||
dynamicInformerFactory := dynamicinformer.NewDynamicSharedInformerFactory(fakeDynamicClient, 0)
|
||||
|
||||
fakeClusterClient := fakecluster.NewSimpleClientset()
|
||||
clusterInformers := clusterv1informers.NewSharedInformerFactory(fakeClusterClient, 10*time.Minute)
|
||||
|
||||
fakeWorkClient := fakework.NewSimpleClientset()
|
||||
workInformers := workinformers.NewSharedInformerFactory(fakeWorkClient, 10*time.Minute)
|
||||
|
||||
hubKubeClient := fakekube.NewSimpleClientset()
|
||||
|
||||
controller := NewAddonTemplateController(
|
||||
nil,
|
||||
hubKubeClient,
|
||||
fakeAddonClient,
|
||||
addonInformers,
|
||||
clusterInformers,
|
||||
dynamicInformerFactory,
|
||||
workInformers,
|
||||
eventstesting.NewTestingEventRecorder(t),
|
||||
runController,
|
||||
)
|
||||
ctx := context.TODO()
|
||||
for _, syncKey := range c.syncKeys {
|
||||
syncContext := testingcommon.NewFakeSyncContext(t, syncKey)
|
||||
err := controller.Sync(ctx, syncContext)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error when sync: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
ch := make(chan struct{})
|
||||
go func() {
|
||||
defer close(ch)
|
||||
wg.Wait()
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-ch:
|
||||
actualCount := rederCount()
|
||||
if actualCount != c.expectedCount {
|
||||
t.Errorf("name : %s, expected runControllerFunc to be called %d, but was called %d times",
|
||||
c.name, c.expectedCount, actualCount)
|
||||
}
|
||||
case <-time.After(1 * time.Second):
|
||||
if !c.expectedTimeout {
|
||||
t.Errorf("name : %s, expected not timeout, but timeout", c.name)
|
||||
}
|
||||
actualCount := rederCount()
|
||||
if actualCount != c.expectedCount {
|
||||
t.Errorf("name : %s, expected runControllerFunc to be called %d, but was called %d times",
|
||||
c.name, c.expectedCount, actualCount)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
package managementaddoninstallprogression
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
jsonpatch "github.com/evanphx/json-patch"
|
||||
"github.com/openshift/library-go/pkg/controller/factory"
|
||||
"github.com/openshift/library-go/pkg/operator/events"
|
||||
"k8s.io/apimachinery/pkg/api/equality"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/klog/v2"
|
||||
|
||||
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
addonv1alpha1client "open-cluster-management.io/api/client/addon/clientset/versioned"
|
||||
addoninformerv1alpha1 "open-cluster-management.io/api/client/addon/informers/externalversions/addon/v1alpha1"
|
||||
addonlisterv1alpha1 "open-cluster-management.io/api/client/addon/listers/addon/v1alpha1"
|
||||
)
|
||||
|
||||
// managementAddonInstallProgressionController reconciles instances of clustermanagementaddon the hub
|
||||
// based to update related object and status condition.
|
||||
type managementAddonInstallProgressionController struct {
|
||||
addonClient addonv1alpha1client.Interface
|
||||
managedClusterAddonLister addonlisterv1alpha1.ManagedClusterAddOnLister
|
||||
clusterManagementAddonLister addonlisterv1alpha1.ClusterManagementAddOnLister
|
||||
addonFilterFunc factory.EventFilterFunc
|
||||
}
|
||||
|
||||
func NewManagementAddonInstallProgressionController(
|
||||
addonClient addonv1alpha1client.Interface,
|
||||
addonInformers addoninformerv1alpha1.ManagedClusterAddOnInformer,
|
||||
clusterManagementAddonInformers addoninformerv1alpha1.ClusterManagementAddOnInformer,
|
||||
addonFilterFunc factory.EventFilterFunc,
|
||||
recorder events.Recorder,
|
||||
) factory.Controller {
|
||||
c := &managementAddonInstallProgressionController{
|
||||
addonClient: addonClient,
|
||||
managedClusterAddonLister: addonInformers.Lister(),
|
||||
clusterManagementAddonLister: clusterManagementAddonInformers.Lister(),
|
||||
addonFilterFunc: addonFilterFunc,
|
||||
}
|
||||
|
||||
return factory.New().WithInformersQueueKeysFunc(
|
||||
func(obj runtime.Object) []string {
|
||||
accessor, _ := meta.Accessor(obj)
|
||||
return []string{accessor.GetName()}
|
||||
},
|
||||
addonInformers.Informer(), clusterManagementAddonInformers.Informer()).
|
||||
WithSync(c.sync).ToController("management-addon-status-controller", recorder)
|
||||
|
||||
}
|
||||
|
||||
func (c *managementAddonInstallProgressionController) sync(ctx context.Context, syncCtx factory.SyncContext) error {
|
||||
addonName := syncCtx.QueueKey()
|
||||
klog.V(4).Infof("Reconciling addon %q", addonName)
|
||||
|
||||
mgmtAddon, err := c.clusterManagementAddonLister.Get(addonName)
|
||||
switch {
|
||||
case errors.IsNotFound(err):
|
||||
return nil
|
||||
case err != nil:
|
||||
return err
|
||||
}
|
||||
|
||||
mgmtAddonCopy := mgmtAddon.DeepCopy()
|
||||
|
||||
clusterManagementAddon, err := c.clusterManagementAddonLister.Get(addonName)
|
||||
if errors.IsNotFound(err) {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// set default config reference
|
||||
mgmtAddonCopy.Status.DefaultConfigReferences = setDefaultConfigReference(mgmtAddonCopy.Spec.SupportedConfigs, mgmtAddonCopy.Status.DefaultConfigReferences)
|
||||
|
||||
// update default config reference when type is manual
|
||||
if mgmtAddonCopy.Spec.InstallStrategy.Type == "" || mgmtAddonCopy.Spec.InstallStrategy.Type == addonv1alpha1.AddonInstallStrategyManual {
|
||||
mgmtAddonCopy.Status.InstallProgressions = []addonv1alpha1.InstallProgression{}
|
||||
return c.patchMgmtAddonStatus(ctx, mgmtAddonCopy, mgmtAddon)
|
||||
}
|
||||
|
||||
// only update default config references and skip updating install progression for self-managed addon
|
||||
if !c.addonFilterFunc(clusterManagementAddon) {
|
||||
return c.patchMgmtAddonStatus(ctx, mgmtAddonCopy, mgmtAddon)
|
||||
}
|
||||
|
||||
// set install progression
|
||||
mgmtAddonCopy.Status.InstallProgressions = setInstallProgression(mgmtAddonCopy.Spec.SupportedConfigs,
|
||||
mgmtAddonCopy.Spec.InstallStrategy.Placements, mgmtAddonCopy.Status.InstallProgressions)
|
||||
|
||||
// update cma status
|
||||
return c.patchMgmtAddonStatus(ctx, mgmtAddonCopy, mgmtAddon)
|
||||
}
|
||||
|
||||
func (c *managementAddonInstallProgressionController) patchMgmtAddonStatus(ctx context.Context, new, old *addonv1alpha1.ClusterManagementAddOn) error {
|
||||
if equality.Semantic.DeepEqual(new.Status, old.Status) {
|
||||
return nil
|
||||
}
|
||||
|
||||
oldData, err := json.Marshal(&addonv1alpha1.ClusterManagementAddOn{
|
||||
Status: addonv1alpha1.ClusterManagementAddOnStatus{
|
||||
DefaultConfigReferences: old.Status.DefaultConfigReferences,
|
||||
InstallProgressions: old.Status.InstallProgressions,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
newData, err := json.Marshal(&addonv1alpha1.ClusterManagementAddOn{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
UID: new.UID,
|
||||
ResourceVersion: new.ResourceVersion,
|
||||
},
|
||||
Status: addonv1alpha1.ClusterManagementAddOnStatus{
|
||||
DefaultConfigReferences: new.Status.DefaultConfigReferences,
|
||||
InstallProgressions: new.Status.InstallProgressions,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
patchBytes, err := jsonpatch.CreateMergePatch(oldData, newData)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create patch for addon %s: %w", new.Name, err)
|
||||
}
|
||||
|
||||
klog.V(2).Infof("Patching clustermanagementaddon %s status with %s", new.Name, string(patchBytes))
|
||||
_, err = c.addonClient.AddonV1alpha1().ClusterManagementAddOns().Patch(
|
||||
ctx, new.Name, types.MergePatchType, patchBytes, metav1.PatchOptions{}, "status")
|
||||
return err
|
||||
}
|
||||
|
||||
func setDefaultConfigReference(supportedConfigs []addonv1alpha1.ConfigMeta,
|
||||
existDefaultConfigReferences []addonv1alpha1.DefaultConfigReference) []addonv1alpha1.DefaultConfigReference {
|
||||
newDefaultConfigReferences := []addonv1alpha1.DefaultConfigReference{}
|
||||
for _, config := range supportedConfigs {
|
||||
if config.DefaultConfig == nil {
|
||||
continue
|
||||
}
|
||||
configRef := addonv1alpha1.DefaultConfigReference{
|
||||
ConfigGroupResource: config.ConfigGroupResource,
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: *config.DefaultConfig,
|
||||
},
|
||||
}
|
||||
// if the config already exists in status, keep the existing spec hash
|
||||
if existConfigRef, exist := findDefaultConfigReference(&configRef, existDefaultConfigReferences); exist {
|
||||
configRef.DesiredConfig.SpecHash = existConfigRef.DesiredConfig.SpecHash
|
||||
}
|
||||
newDefaultConfigReferences = append(newDefaultConfigReferences, configRef)
|
||||
}
|
||||
return newDefaultConfigReferences
|
||||
}
|
||||
|
||||
func findDefaultConfigReference(
|
||||
newobj *addonv1alpha1.DefaultConfigReference,
|
||||
oldobjs []addonv1alpha1.DefaultConfigReference,
|
||||
) (*addonv1alpha1.DefaultConfigReference, bool) {
|
||||
for _, oldconfig := range oldobjs {
|
||||
if oldconfig.ConfigGroupResource == newobj.ConfigGroupResource && oldconfig.DesiredConfig.ConfigReferent == newobj.DesiredConfig.ConfigReferent {
|
||||
return &oldconfig, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func setInstallProgression(supportedConfigs []addonv1alpha1.ConfigMeta, placementStrategies []addonv1alpha1.PlacementStrategy,
|
||||
existInstallProgressions []addonv1alpha1.InstallProgression) []addonv1alpha1.InstallProgression {
|
||||
newInstallProgressions := []addonv1alpha1.InstallProgression{}
|
||||
for _, placementStrategy := range placementStrategies {
|
||||
// set placement ref
|
||||
installProgression := addonv1alpha1.InstallProgression{
|
||||
PlacementRef: placementStrategy.PlacementRef,
|
||||
}
|
||||
|
||||
// set config references as default configuration
|
||||
installConfigReferences := []addonv1alpha1.InstallConfigReference{}
|
||||
installConfigReferencesMap := map[addonv1alpha1.ConfigGroupResource]addonv1alpha1.ConfigReferent{}
|
||||
for _, config := range supportedConfigs {
|
||||
if config.DefaultConfig != nil {
|
||||
installConfigReferencesMap[config.ConfigGroupResource] = *config.DefaultConfig
|
||||
}
|
||||
}
|
||||
|
||||
// override the default configuration for each placement
|
||||
for _, config := range placementStrategy.Configs {
|
||||
installConfigReferencesMap[config.ConfigGroupResource] = config.ConfigReferent
|
||||
}
|
||||
|
||||
// set the config references for each install progression
|
||||
for k, v := range installConfigReferencesMap {
|
||||
installConfigReferences = append(installConfigReferences,
|
||||
addonv1alpha1.InstallConfigReference{
|
||||
ConfigGroupResource: k,
|
||||
DesiredConfig: &addonv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: v,
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
installProgression.ConfigReferences = installConfigReferences
|
||||
|
||||
// if the config group resource already exists in status, merge the install progression
|
||||
if existInstallProgression, exist := findInstallProgression(&installProgression, existInstallProgressions); exist {
|
||||
mergeInstallProgression(&installProgression, existInstallProgression)
|
||||
}
|
||||
newInstallProgressions = append(newInstallProgressions, installProgression)
|
||||
}
|
||||
return newInstallProgressions
|
||||
}
|
||||
|
||||
func findInstallProgression(newobj *addonv1alpha1.InstallProgression, oldobjs []addonv1alpha1.InstallProgression) (*addonv1alpha1.InstallProgression, bool) {
|
||||
for _, oldobj := range oldobjs {
|
||||
if oldobj.PlacementRef == newobj.PlacementRef {
|
||||
count := 0
|
||||
for _, oldconfig := range oldobj.ConfigReferences {
|
||||
for _, newconfig := range newobj.ConfigReferences {
|
||||
if oldconfig.ConfigGroupResource == newconfig.ConfigGroupResource {
|
||||
count += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
if count == len(newobj.ConfigReferences) {
|
||||
return &oldobj, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func mergeInstallProgression(newobj, oldobj *addonv1alpha1.InstallProgression) {
|
||||
// merge config reference
|
||||
for i := range newobj.ConfigReferences {
|
||||
for _, oldconfig := range oldobj.ConfigReferences {
|
||||
if newobj.ConfigReferences[i].ConfigGroupResource == oldconfig.ConfigGroupResource {
|
||||
if newobj.ConfigReferences[i].DesiredConfig.ConfigReferent == oldconfig.DesiredConfig.ConfigReferent {
|
||||
newobj.ConfigReferences[i].DesiredConfig.SpecHash = oldconfig.DesiredConfig.SpecHash
|
||||
}
|
||||
newobj.ConfigReferences[i].LastAppliedConfig = oldconfig.LastAppliedConfig.DeepCopy()
|
||||
newobj.ConfigReferences[i].LastKnownGoodConfig = oldconfig.LastKnownGoodConfig.DeepCopy()
|
||||
}
|
||||
}
|
||||
}
|
||||
newobj.Conditions = oldobj.Conditions
|
||||
}
|
||||
@@ -0,0 +1,259 @@
|
||||
package managementaddoninstallprogression
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
clienttesting "k8s.io/client-go/testing"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/addonmanager/addontesting"
|
||||
"open-cluster-management.io/addon-framework/pkg/agent"
|
||||
"open-cluster-management.io/addon-framework/pkg/utils"
|
||||
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
fakeaddon "open-cluster-management.io/api/client/addon/clientset/versioned/fake"
|
||||
addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions"
|
||||
|
||||
testingcommon "open-cluster-management.io/ocm/pkg/common/testing"
|
||||
)
|
||||
|
||||
func TestReconcile(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
syncKey string
|
||||
managedClusteraddon []runtime.Object
|
||||
clusterManagementAddon []runtime.Object
|
||||
validateAddonActions func(t *testing.T, actions []clienttesting.Action)
|
||||
}{
|
||||
{
|
||||
name: "no clustermanagementaddon",
|
||||
syncKey: "test",
|
||||
managedClusteraddon: []runtime.Object{},
|
||||
clusterManagementAddon: []runtime.Object{},
|
||||
validateAddonActions: addontesting.AssertNoActions,
|
||||
},
|
||||
{
|
||||
name: "update clustermanagementaddon status with type manual with no configs",
|
||||
syncKey: "test",
|
||||
managedClusteraddon: []runtime.Object{},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "testcrd", "testcr").Build()},
|
||||
validateAddonActions: addontesting.AssertNoActions,
|
||||
},
|
||||
{
|
||||
name: "update clustermanagementaddon status with type manual with supported configs",
|
||||
syncKey: "test",
|
||||
managedClusteraddon: []runtime.Object{},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "testcrd", "testcr").WithSupportedConfigs(
|
||||
addonapiv1alpha1.ConfigMeta{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: "addon.open-cluster-management.io",
|
||||
Resource: "addonhubconfigs",
|
||||
},
|
||||
DefaultConfig: &addonapiv1alpha1.ConfigReferent{
|
||||
Name: "test",
|
||||
Namespace: "test",
|
||||
},
|
||||
}).Build()},
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch")
|
||||
actual := actions[0].(clienttesting.PatchActionImpl).Patch
|
||||
cma := &addonapiv1alpha1.ClusterManagementAddOn{}
|
||||
err := json.Unmarshal(actual, cma)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(cma.Status.DefaultConfigReferences) != 1 {
|
||||
t.Errorf("DefaultConfigReferences object is not correct: %v", cma.Status.DefaultConfigReferences)
|
||||
}
|
||||
if len(cma.Status.InstallProgressions) != 0 {
|
||||
t.Errorf("InstallProgressions object is not correct: %v", cma.Status.InstallProgressions)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "update clustermanagementaddon status with type manual with invalid supported configs",
|
||||
syncKey: "test",
|
||||
managedClusteraddon: []runtime.Object{},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "testcrd", "testcr").WithSupportedConfigs(
|
||||
addonapiv1alpha1.ConfigMeta{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: "addon.open-cluster-management.io",
|
||||
Resource: "addonhubconfigs",
|
||||
},
|
||||
}).Build()},
|
||||
validateAddonActions: addontesting.AssertNoActions,
|
||||
},
|
||||
{
|
||||
name: "update clustermanagementaddon status with type placements",
|
||||
syncKey: "test",
|
||||
managedClusteraddon: []runtime.Object{},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "testcrd", "testcr").WithPlacementStrategy(
|
||||
addonapiv1alpha1.PlacementStrategy{
|
||||
PlacementRef: addonapiv1alpha1.PlacementRef{
|
||||
Name: "placement1",
|
||||
Namespace: "test",
|
||||
},
|
||||
},
|
||||
addonapiv1alpha1.PlacementStrategy{
|
||||
PlacementRef: addonapiv1alpha1.PlacementRef{
|
||||
Name: "placement2",
|
||||
Namespace: "test",
|
||||
},
|
||||
Configs: []addonapiv1alpha1.AddOnConfig{
|
||||
{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: "addon.open-cluster-management.io",
|
||||
Resource: "addondeploymentconfigs",
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Name: "test",
|
||||
Namespace: "test",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
).Build()},
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch")
|
||||
actual := actions[0].(clienttesting.PatchActionImpl).Patch
|
||||
cma := &addonapiv1alpha1.ClusterManagementAddOn{}
|
||||
err := json.Unmarshal(actual, cma)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(cma.Status.DefaultConfigReferences) != 0 {
|
||||
t.Errorf("DefaultConfigReferences object is not correct: %v", cma.Status.DefaultConfigReferences)
|
||||
}
|
||||
if len(cma.Status.InstallProgressions) != 2 {
|
||||
t.Errorf("InstallProgressions object is not correct: %v", cma.Status.InstallProgressions)
|
||||
}
|
||||
if len(cma.Status.InstallProgressions[0].ConfigReferences) != 0 {
|
||||
t.Errorf("InstallProgressions ConfigReferences object is not correct: %v", cma.Status.InstallProgressions[0].ConfigReferences)
|
||||
}
|
||||
if len(cma.Status.InstallProgressions[1].ConfigReferences) != 1 {
|
||||
t.Errorf("InstallProgressions ConfigReferences object is not correct: %v", cma.Status.InstallProgressions[0].ConfigReferences)
|
||||
}
|
||||
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "update clustermanagementaddon status with type placements and default configs",
|
||||
syncKey: "test",
|
||||
managedClusteraddon: []runtime.Object{},
|
||||
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "testcrd", "testcr").WithPlacementStrategy(
|
||||
addonapiv1alpha1.PlacementStrategy{
|
||||
PlacementRef: addonapiv1alpha1.PlacementRef{
|
||||
Name: "placement1",
|
||||
Namespace: "test",
|
||||
},
|
||||
Configs: []addonapiv1alpha1.AddOnConfig{
|
||||
{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: "addon.open-cluster-management.io",
|
||||
Resource: "addonhubconfigs",
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Name: "test1",
|
||||
Namespace: "test",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
addonapiv1alpha1.PlacementStrategy{
|
||||
PlacementRef: addonapiv1alpha1.PlacementRef{
|
||||
Name: "placement2",
|
||||
Namespace: "test",
|
||||
},
|
||||
Configs: []addonapiv1alpha1.AddOnConfig{
|
||||
{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: "addon.open-cluster-management.io",
|
||||
Resource: "addondeploymentconfigs",
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Name: "test",
|
||||
Namespace: "test",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
).WithSupportedConfigs(
|
||||
addonapiv1alpha1.ConfigMeta{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: "addon.open-cluster-management.io",
|
||||
Resource: "addonhubconfigs",
|
||||
},
|
||||
DefaultConfig: &addonapiv1alpha1.ConfigReferent{
|
||||
Name: "test",
|
||||
Namespace: "test",
|
||||
},
|
||||
}).Build()},
|
||||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
|
||||
addontesting.AssertActions(t, actions, "patch")
|
||||
actual := actions[0].(clienttesting.PatchActionImpl).Patch
|
||||
cma := &addonapiv1alpha1.ClusterManagementAddOn{}
|
||||
err := json.Unmarshal(actual, cma)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(cma.Status.DefaultConfigReferences) != 1 {
|
||||
t.Errorf("DefaultConfigReferences object is not correct: %v", cma.Status.DefaultConfigReferences)
|
||||
}
|
||||
if len(cma.Status.InstallProgressions) != 2 {
|
||||
t.Errorf("InstallProgressions object is not correct: %v", cma.Status.InstallProgressions)
|
||||
}
|
||||
if len(cma.Status.InstallProgressions[0].ConfigReferences) != 1 {
|
||||
t.Errorf("InstallProgressions ConfigReferences object is not correct: %v", cma.Status.InstallProgressions[0].ConfigReferences)
|
||||
}
|
||||
if cma.Status.InstallProgressions[0].ConfigReferences[0].DesiredConfig.Name != "test1" {
|
||||
t.Errorf("InstallProgressions ConfigReferences object is not correct: %v", cma.Status.InstallProgressions[0].ConfigReferences[0].DesiredConfig.Name)
|
||||
}
|
||||
if len(cma.Status.InstallProgressions[1].ConfigReferences) != 2 {
|
||||
t.Errorf("InstallProgressions ConfigReferences object is not correct: %v", cma.Status.InstallProgressions[0].ConfigReferences)
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
obj := append(c.clusterManagementAddon, c.managedClusteraddon...)
|
||||
fakeAddonClient := fakeaddon.NewSimpleClientset(obj...)
|
||||
|
||||
addonInformers := addoninformers.NewSharedInformerFactory(fakeAddonClient, 10*time.Minute)
|
||||
|
||||
for _, obj := range c.managedClusteraddon {
|
||||
if err := addonInformers.Addon().V1alpha1().ManagedClusterAddOns().Informer().GetStore().Add(obj); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for _, obj := range c.clusterManagementAddon {
|
||||
if err := addonInformers.Addon().V1alpha1().ClusterManagementAddOns().Informer().GetStore().Add(obj); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
syncContext := testingcommon.NewFakeSyncContext(t, c.syncKey)
|
||||
recorder := syncContext.Recorder()
|
||||
|
||||
controller := NewManagementAddonInstallProgressionController(
|
||||
fakeAddonClient,
|
||||
addonInformers.Addon().V1alpha1().ManagedClusterAddOns(),
|
||||
addonInformers.Addon().V1alpha1().ClusterManagementAddOns(),
|
||||
utils.ManagedBySelf(map[string]agent.AgentAddon{"test": nil}),
|
||||
recorder,
|
||||
)
|
||||
|
||||
err := controller.Sync(context.TODO(), syncContext)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error when sync: %v", err)
|
||||
}
|
||||
c.validateAddonActions(t, fakeAddonClient.Actions())
|
||||
})
|
||||
}
|
||||
}
|
||||
191
pkg/addon/manager.go
Normal file
191
pkg/addon/manager.go
Normal file
@@ -0,0 +1,191 @@
|
||||
package addon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/openshift/library-go/pkg/controller/controllercmd"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/dynamic"
|
||||
"k8s.io/client-go/dynamic/dynamicinformer"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/index"
|
||||
"open-cluster-management.io/addon-framework/pkg/utils"
|
||||
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
addonv1alpha1client "open-cluster-management.io/api/client/addon/clientset/versioned"
|
||||
addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions"
|
||||
clusterclientset "open-cluster-management.io/api/client/cluster/clientset/versioned"
|
||||
clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions"
|
||||
workv1client "open-cluster-management.io/api/client/work/clientset/versioned"
|
||||
workv1informers "open-cluster-management.io/api/client/work/informers/externalversions"
|
||||
|
||||
"open-cluster-management.io/ocm/pkg/addon/controllers/addonconfiguration"
|
||||
"open-cluster-management.io/ocm/pkg/addon/controllers/addonmanagement"
|
||||
"open-cluster-management.io/ocm/pkg/addon/controllers/addonowner"
|
||||
"open-cluster-management.io/ocm/pkg/addon/controllers/addonprogressing"
|
||||
"open-cluster-management.io/ocm/pkg/addon/controllers/addontemplate"
|
||||
"open-cluster-management.io/ocm/pkg/addon/controllers/managementaddoninstallprogression"
|
||||
)
|
||||
|
||||
func RunManager(ctx context.Context, controllerContext *controllercmd.ControllerContext) error {
|
||||
kubeConfig := controllerContext.KubeConfig
|
||||
hubKubeClient, err := kubernetes.NewForConfig(kubeConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hubClusterClient, err := clusterclientset.NewForConfig(kubeConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
addonClient, err := addonv1alpha1client.NewForConfig(kubeConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
workClient, err := workv1client.NewForConfig(kubeConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dynamicClient, err := dynamic.NewForConfig(kubeConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
clusterInformerFactory := clusterinformers.NewSharedInformerFactory(hubClusterClient, 30*time.Minute)
|
||||
addonInformerFactory := addoninformers.NewSharedInformerFactory(addonClient, 30*time.Minute)
|
||||
workInformers := workv1informers.NewSharedInformerFactoryWithOptions(workClient, 10*time.Minute,
|
||||
workv1informers.WithTweakListOptions(func(listOptions *metav1.ListOptions) {
|
||||
selector := &metav1.LabelSelector{
|
||||
MatchExpressions: []metav1.LabelSelectorRequirement{
|
||||
{
|
||||
Key: addonv1alpha1.AddonLabelKey,
|
||||
Operator: metav1.LabelSelectorOpExists,
|
||||
},
|
||||
},
|
||||
}
|
||||
listOptions.LabelSelector = metav1.FormatLabelSelector(selector)
|
||||
}),
|
||||
)
|
||||
|
||||
// addonDeployController
|
||||
err = workInformers.Work().V1().ManifestWorks().Informer().AddIndexers(
|
||||
cache.Indexers{
|
||||
index.ManifestWorkByAddon: index.IndexManifestWorkByAddon,
|
||||
index.ManifestWorkByHostedAddon: index.IndexManifestWorkByHostedAddon,
|
||||
index.ManifestWorkHookByHostedAddon: index.IndexManifestWorkHookByHostedAddon,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// addonConfigController
|
||||
err = addonInformerFactory.Addon().V1alpha1().ManagedClusterAddOns().Informer().AddIndexers(
|
||||
cache.Indexers{index.AddonByConfig: index.IndexAddonByConfig},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// managementAddonConfigController
|
||||
err = addonInformerFactory.Addon().V1alpha1().ClusterManagementAddOns().Informer().AddIndexers(
|
||||
cache.Indexers{index.ClusterManagementAddonByConfig: index.IndexClusterManagementAddonByConfig})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = addonInformerFactory.Addon().V1alpha1().ClusterManagementAddOns().Informer().AddIndexers(
|
||||
cache.Indexers{
|
||||
index.ClusterManagementAddonByPlacement: index.IndexClusterManagementAddonByPlacement,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = addonInformerFactory.Addon().V1alpha1().ManagedClusterAddOns().Informer().AddIndexers(
|
||||
cache.Indexers{
|
||||
index.ManagedClusterAddonByName: index.IndexManagedClusterAddonByName,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dynamicInformers := dynamicinformer.NewDynamicSharedInformerFactory(dynamicClient, 10*time.Minute)
|
||||
|
||||
addonManagementController := addonmanagement.NewAddonManagementController(
|
||||
addonClient,
|
||||
addonInformerFactory.Addon().V1alpha1().ManagedClusterAddOns(),
|
||||
addonInformerFactory.Addon().V1alpha1().ClusterManagementAddOns(),
|
||||
clusterInformerFactory.Cluster().V1beta1().Placements(),
|
||||
clusterInformerFactory.Cluster().V1beta1().PlacementDecisions(),
|
||||
utils.ManagedByAddonManager,
|
||||
controllerContext.EventRecorder,
|
||||
)
|
||||
|
||||
addonConfigurationController := addonconfiguration.NewAddonConfigurationController(
|
||||
addonClient,
|
||||
addonInformerFactory.Addon().V1alpha1().ManagedClusterAddOns(),
|
||||
addonInformerFactory.Addon().V1alpha1().ClusterManagementAddOns(),
|
||||
clusterInformerFactory.Cluster().V1beta1().Placements(),
|
||||
clusterInformerFactory.Cluster().V1beta1().PlacementDecisions(),
|
||||
utils.ManagedByAddonManager,
|
||||
controllerContext.EventRecorder,
|
||||
)
|
||||
|
||||
addonOwnerController := addonowner.NewAddonOwnerController(
|
||||
addonClient,
|
||||
addonInformerFactory.Addon().V1alpha1().ManagedClusterAddOns(),
|
||||
addonInformerFactory.Addon().V1alpha1().ClusterManagementAddOns(),
|
||||
utils.ManagedByAddonManager,
|
||||
controllerContext.EventRecorder,
|
||||
)
|
||||
|
||||
addonProgressingController := addonprogressing.NewAddonProgressingController(
|
||||
addonClient,
|
||||
addonInformerFactory.Addon().V1alpha1().ManagedClusterAddOns(),
|
||||
addonInformerFactory.Addon().V1alpha1().ClusterManagementAddOns(),
|
||||
workInformers.Work().V1().ManifestWorks(),
|
||||
utils.ManagedByAddonManager,
|
||||
controllerContext.EventRecorder,
|
||||
)
|
||||
|
||||
mgmtAddonInstallProgressionController := managementaddoninstallprogression.NewManagementAddonInstallProgressionController(
|
||||
addonClient,
|
||||
addonInformerFactory.Addon().V1alpha1().ManagedClusterAddOns(),
|
||||
addonInformerFactory.Addon().V1alpha1().ClusterManagementAddOns(),
|
||||
utils.ManagedByAddonManager,
|
||||
controllerContext.EventRecorder,
|
||||
)
|
||||
|
||||
addonTemplateController := addontemplate.NewAddonTemplateController(
|
||||
kubeConfig,
|
||||
hubKubeClient,
|
||||
addonClient,
|
||||
addonInformerFactory,
|
||||
clusterInformerFactory,
|
||||
dynamicInformers,
|
||||
workInformers,
|
||||
controllerContext.EventRecorder,
|
||||
)
|
||||
|
||||
go addonManagementController.Run(ctx, 2)
|
||||
go addonConfigurationController.Run(ctx, 2)
|
||||
go addonOwnerController.Run(ctx, 2)
|
||||
go addonProgressingController.Run(ctx, 2)
|
||||
go mgmtAddonInstallProgressionController.Run(ctx, 2)
|
||||
// There should be only one instance of addonTemplateController running, since the addonTemplateController will
|
||||
// start a goroutine for each template-type addon it watches.
|
||||
go addonTemplateController.Run(ctx, 1)
|
||||
|
||||
clusterInformerFactory.Start(ctx.Done())
|
||||
addonInformerFactory.Start(ctx.Done())
|
||||
workInformers.Start(ctx.Done())
|
||||
dynamicInformers.Start(ctx.Done())
|
||||
|
||||
<-ctx.Done()
|
||||
return nil
|
||||
}
|
||||
49
pkg/addon/templateagent/common.go
Normal file
49
pkg/addon/templateagent/common.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package templateagent
|
||||
|
||||
import (
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/utils"
|
||||
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
)
|
||||
|
||||
// AddonTemplateConfigRef return the first addon template config
|
||||
func AddonTemplateConfigRef(
|
||||
configReferences []addonapiv1alpha1.ConfigReference) (bool, addonapiv1alpha1.ConfigReference) {
|
||||
for _, config := range configReferences {
|
||||
if config.Group == utils.AddOnTemplateGVR.Group && config.Resource == utils.AddOnTemplateGVR.Resource {
|
||||
return true, config
|
||||
}
|
||||
}
|
||||
return false, addonapiv1alpha1.ConfigReference{}
|
||||
}
|
||||
|
||||
// GetTemplateSpecHash returns the sha256 hash of the spec field of the addon template
|
||||
func GetTemplateSpecHash(template *addonapiv1alpha1.AddOnTemplate) (string, error) {
|
||||
unstructuredTemplate, err := runtime.DefaultUnstructuredConverter.ToUnstructured(template)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
specHash, err := utils.GetSpecHash(&unstructured.Unstructured{
|
||||
Object: unstructuredTemplate,
|
||||
})
|
||||
if err != nil {
|
||||
return specHash, err
|
||||
}
|
||||
return specHash, nil
|
||||
}
|
||||
|
||||
// SupportAddOnTemplate return true if the given ClusterManagementAddOn supports the AddOnTemplate
|
||||
func SupportAddOnTemplate(cma *addonapiv1alpha1.ClusterManagementAddOn) bool {
|
||||
if cma == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, config := range cma.Spec.SupportedConfigs {
|
||||
if config.Group == utils.AddOnTemplateGVR.Group && config.Resource == utils.AddOnTemplateGVR.Resource {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
190
pkg/addon/templateagent/decorator.go
Normal file
190
pkg/addon/templateagent/decorator.go
Normal file
@@ -0,0 +1,190 @@
|
||||
package templateagent
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/addonfactory"
|
||||
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
)
|
||||
|
||||
type deploymentDecorator interface {
|
||||
// decorate modifies the deployment in place
|
||||
decorate(deployment *appsv1.Deployment) error
|
||||
}
|
||||
|
||||
type environmentDecorator struct {
|
||||
orderedValues orderedValues
|
||||
}
|
||||
|
||||
func newEnvironmentDecorator(orderedValues orderedValues) deploymentDecorator {
|
||||
return &environmentDecorator{
|
||||
orderedValues: orderedValues,
|
||||
}
|
||||
}
|
||||
func (d *environmentDecorator) decorate(deployment *appsv1.Deployment) error {
|
||||
envVars := make([]corev1.EnvVar, len(d.orderedValues))
|
||||
for index, value := range d.orderedValues {
|
||||
envVars[index] = corev1.EnvVar{
|
||||
Name: value.name,
|
||||
Value: value.value,
|
||||
}
|
||||
}
|
||||
|
||||
for j := range deployment.Spec.Template.Spec.Containers {
|
||||
deployment.Spec.Template.Spec.Containers[j].Env = append(
|
||||
deployment.Spec.Template.Spec.Containers[j].Env,
|
||||
envVars...)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type volumeDecorator struct {
|
||||
template *addonapiv1alpha1.AddOnTemplate
|
||||
addonName string
|
||||
}
|
||||
|
||||
func newVolumeDecorator(addonName string, template *addonapiv1alpha1.AddOnTemplate) deploymentDecorator {
|
||||
return &volumeDecorator{
|
||||
addonName: addonName,
|
||||
template: template,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *volumeDecorator) decorate(deployment *appsv1.Deployment) error {
|
||||
|
||||
volumeMounts := []corev1.VolumeMount{}
|
||||
volumes := []corev1.Volume{}
|
||||
|
||||
for _, registration := range d.template.Spec.Registration {
|
||||
if registration.Type == addonapiv1alpha1.RegistrationTypeKubeClient {
|
||||
volumeMounts = append(volumeMounts, corev1.VolumeMount{
|
||||
Name: "hub-kubeconfig",
|
||||
MountPath: hubKubeconfigSecretMountPath(),
|
||||
})
|
||||
volumes = append(volumes, corev1.Volume{
|
||||
Name: "hub-kubeconfig",
|
||||
VolumeSource: corev1.VolumeSource{
|
||||
Secret: &corev1.SecretVolumeSource{
|
||||
SecretName: hubKubeconfigSecretName(d.addonName),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
if registration.Type == addonapiv1alpha1.RegistrationTypeCustomSigner {
|
||||
if registration.CustomSigner == nil {
|
||||
return fmt.Errorf("custom signer is nil")
|
||||
}
|
||||
name := fmt.Sprintf("cert-%s", strings.ReplaceAll(
|
||||
strings.ReplaceAll(registration.CustomSigner.SignerName, "/", "-"),
|
||||
".", "-"))
|
||||
volumeMounts = append(volumeMounts, corev1.VolumeMount{
|
||||
Name: name,
|
||||
MountPath: customSignedSecretMountPath(registration.CustomSigner.SignerName),
|
||||
})
|
||||
volumes = append(volumes, corev1.Volume{
|
||||
Name: name,
|
||||
VolumeSource: corev1.VolumeSource{
|
||||
Secret: &corev1.SecretVolumeSource{
|
||||
SecretName: getCustomSignedSecretName(d.addonName, registration.CustomSigner.SignerName),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if len(volumeMounts) == 0 || len(volumes) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
for j := range deployment.Spec.Template.Spec.Containers {
|
||||
deployment.Spec.Template.Spec.Containers[j].VolumeMounts = append(
|
||||
deployment.Spec.Template.Spec.Containers[j].VolumeMounts, volumeMounts...)
|
||||
}
|
||||
|
||||
deployment.Spec.Template.Spec.Volumes = append(deployment.Spec.Template.Spec.Volumes, volumes...)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type nodePlacementDecorator struct {
|
||||
privateValues addonfactory.Values
|
||||
}
|
||||
|
||||
func newNodePlacementDecorator(privateValues addonfactory.Values) deploymentDecorator {
|
||||
return &nodePlacementDecorator{
|
||||
privateValues: privateValues,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *nodePlacementDecorator) decorate(deployment *appsv1.Deployment) error {
|
||||
nodePlacement, ok := d.privateValues[NodePlacementPrivateValueKey]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
np, ok := nodePlacement.(*addonapiv1alpha1.NodePlacement)
|
||||
if !ok {
|
||||
return fmt.Errorf("node placement value is invalid")
|
||||
}
|
||||
|
||||
if np.NodeSelector != nil {
|
||||
deployment.Spec.Template.Spec.NodeSelector = np.NodeSelector
|
||||
}
|
||||
|
||||
if np.NodeSelector != nil {
|
||||
deployment.Spec.Template.Spec.Tolerations = np.Tolerations
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type imageDecorator struct {
|
||||
privateValues addonfactory.Values
|
||||
}
|
||||
|
||||
func newImageDecorator(privateValues addonfactory.Values) deploymentDecorator {
|
||||
return &imageDecorator{
|
||||
privateValues: privateValues,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *imageDecorator) decorate(deployment *appsv1.Deployment) error {
|
||||
registries, ok := d.privateValues[RegistriesPrivateValueKey]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
ims, ok := registries.([]addonapiv1alpha1.ImageMirror)
|
||||
if !ok {
|
||||
return fmt.Errorf("registries value is invalid")
|
||||
}
|
||||
|
||||
for i := range deployment.Spec.Template.Spec.Containers {
|
||||
deployment.Spec.Template.Spec.Containers[i].Image = addonfactory.OverrideImage(
|
||||
ims, deployment.Spec.Template.Spec.Containers[i].Image)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func hubKubeconfigSecretMountPath() string {
|
||||
return "/managed/hub-kubeconfig"
|
||||
}
|
||||
|
||||
func hubKubeconfigSecretName(addonName string) string {
|
||||
return fmt.Sprintf("%s-hub-kubeconfig", addonName)
|
||||
}
|
||||
|
||||
func getCustomSignedSecretName(addonName, signerName string) string {
|
||||
return fmt.Sprintf("%s-%s-client-cert", addonName, strings.ReplaceAll(signerName, "/", "-"))
|
||||
}
|
||||
|
||||
func customSignedSecretMountPath(signerName string) string {
|
||||
return fmt.Sprintf("/managed/%s", strings.ReplaceAll(signerName, "/", "-"))
|
||||
}
|
||||
415
pkg/addon/templateagent/registration.go
Normal file
415
pkg/addon/templateagent/registration.go
Normal file
@@ -0,0 +1,415 @@
|
||||
package templateagent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
openshiftcrypto "github.com/openshift/library-go/pkg/crypto"
|
||||
"github.com/pkg/errors"
|
||||
certificatesv1 "k8s.io/api/certificates/v1"
|
||||
rbacv1 "k8s.io/api/rbac/v1"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/klog/v2"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/agent"
|
||||
"open-cluster-management.io/addon-framework/pkg/utils"
|
||||
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
clusterv1 "open-cluster-management.io/api/cluster/v1"
|
||||
)
|
||||
|
||||
const (
|
||||
TLSCACert = "ca.crt"
|
||||
TLSCAKey = "ca.key"
|
||||
// AddonTemplateLabelKey is the label key to set addon template name. It is to set the resources on the hub relating
|
||||
// to an addon template
|
||||
AddonTemplateLabelKey = "open-cluster-management.io/addon-template-name"
|
||||
)
|
||||
|
||||
func (a *CRDTemplateAgentAddon) GetDesiredAddOnTemplate(addon *addonapiv1alpha1.ManagedClusterAddOn,
|
||||
clusterName, addonName string) (*addonapiv1alpha1.AddOnTemplate, error) {
|
||||
|
||||
if addon == nil {
|
||||
var err error
|
||||
addon, err = a.addonLister.ManagedClusterAddOns(clusterName).Get(addonName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return a.GetDesiredAddOnTemplateByAddon(addon)
|
||||
}
|
||||
|
||||
func (a *CRDTemplateAgentAddon) TemplateCSRConfigurationsFunc() func(cluster *clusterv1.ManagedCluster) []addonapiv1alpha1.RegistrationConfig {
|
||||
|
||||
return func(cluster *clusterv1.ManagedCluster) []addonapiv1alpha1.RegistrationConfig {
|
||||
template, err := a.GetDesiredAddOnTemplate(nil, cluster.Name, a.addonName)
|
||||
if err != nil {
|
||||
utilruntime.HandleError(fmt.Errorf("failed to get addon %s template: %v", a.addonName, err))
|
||||
return nil
|
||||
}
|
||||
if template == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
contain := func(rcs []addonapiv1alpha1.RegistrationConfig, signerName string) bool {
|
||||
for _, rc := range rcs {
|
||||
if rc.SignerName == signerName {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
registrationConfigs := make([]addonapiv1alpha1.RegistrationConfig, 0)
|
||||
for _, registration := range template.Spec.Registration {
|
||||
switch registration.Type {
|
||||
case addonapiv1alpha1.RegistrationTypeKubeClient:
|
||||
if !contain(registrationConfigs, certificatesv1.KubeAPIServerClientSignerName) {
|
||||
configs := agent.KubeClientSignerConfigurations(a.addonName, a.agentName)(cluster)
|
||||
registrationConfigs = append(registrationConfigs, configs...)
|
||||
}
|
||||
|
||||
case addonapiv1alpha1.RegistrationTypeCustomSigner:
|
||||
if registration.CustomSigner == nil {
|
||||
continue
|
||||
}
|
||||
if !contain(registrationConfigs, registration.CustomSigner.SignerName) {
|
||||
configs := CustomSignerConfigurations(
|
||||
a.addonName, a.agentName, registration.CustomSigner)(cluster)
|
||||
registrationConfigs = append(registrationConfigs, configs...)
|
||||
}
|
||||
|
||||
default:
|
||||
utilruntime.HandleError(fmt.Errorf("unsupported registration type %s", registration.Type))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return registrationConfigs
|
||||
}
|
||||
}
|
||||
|
||||
// CustomSignerConfigurations returns a func that can generate RegistrationConfig
|
||||
// for CustomSigner type registration addon
|
||||
func CustomSignerConfigurations(addonName, agentName string,
|
||||
customSignerConfig *addonapiv1alpha1.CustomSignerRegistrationConfig,
|
||||
) func(cluster *clusterv1.ManagedCluster) []addonapiv1alpha1.RegistrationConfig {
|
||||
return func(cluster *clusterv1.ManagedCluster) []addonapiv1alpha1.RegistrationConfig {
|
||||
if customSignerConfig == nil {
|
||||
utilruntime.HandleError(fmt.Errorf("custome signer is nil"))
|
||||
}
|
||||
config := addonapiv1alpha1.RegistrationConfig{
|
||||
SignerName: customSignerConfig.SignerName,
|
||||
// TODO: confirm the subject
|
||||
Subject: addonapiv1alpha1.Subject{
|
||||
User: agent.DefaultUser(cluster.Name, addonName, agentName),
|
||||
Groups: agent.DefaultGroups(cluster.Name, addonName),
|
||||
},
|
||||
}
|
||||
if customSignerConfig.Subject != nil {
|
||||
config.Subject = *customSignerConfig.Subject
|
||||
}
|
||||
|
||||
return []addonapiv1alpha1.RegistrationConfig{config}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *CRDTemplateAgentAddon) TemplateCSRApproveCheckFunc() agent.CSRApproveFunc {
|
||||
|
||||
return func(cluster *clusterv1.ManagedCluster, addon *addonapiv1alpha1.ManagedClusterAddOn,
|
||||
csr *certificatesv1.CertificateSigningRequest) bool {
|
||||
|
||||
template, err := a.GetDesiredAddOnTemplate(addon, cluster.Name, a.addonName)
|
||||
if err != nil {
|
||||
utilruntime.HandleError(fmt.Errorf("failed to get addon %s template: %v", a.addonName, err))
|
||||
return false
|
||||
}
|
||||
if template == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, registration := range template.Spec.Registration {
|
||||
switch registration.Type {
|
||||
case addonapiv1alpha1.RegistrationTypeKubeClient:
|
||||
|
||||
if csr.Spec.SignerName == certificatesv1.KubeAPIServerClientSignerName {
|
||||
return KubeClientCSRApprover(a.agentName)(cluster, addon, csr)
|
||||
}
|
||||
|
||||
case addonapiv1alpha1.RegistrationTypeCustomSigner:
|
||||
if registration.CustomSigner == nil {
|
||||
continue
|
||||
}
|
||||
if csr.Spec.SignerName == registration.CustomSigner.SignerName {
|
||||
return CustomerSignerCSRApprover(a.addonName)(cluster, addon, csr)
|
||||
}
|
||||
|
||||
default:
|
||||
utilruntime.HandleError(fmt.Errorf("unsupported registration type %s", registration.Type))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// KubeClientCSRApprover approve the csr when addon agent uses default group, default user and
|
||||
// "kubernetes.io/kube-apiserver-client" signer to sign csr.
|
||||
func KubeClientCSRApprover(agentName string) agent.CSRApproveFunc {
|
||||
return func(
|
||||
cluster *clusterv1.ManagedCluster,
|
||||
addon *addonapiv1alpha1.ManagedClusterAddOn,
|
||||
csr *certificatesv1.CertificateSigningRequest) bool {
|
||||
if csr.Spec.SignerName != certificatesv1.KubeAPIServerClientSignerName {
|
||||
return false
|
||||
}
|
||||
return utils.DefaultCSRApprover(agentName)(cluster, addon, csr)
|
||||
}
|
||||
}
|
||||
|
||||
// CustomerSignerCSRApprover approve the csr when addon agent uses custom signer to sign csr.
|
||||
func CustomerSignerCSRApprover(agentName string) agent.CSRApproveFunc {
|
||||
return func(
|
||||
cluster *clusterv1.ManagedCluster,
|
||||
addon *addonapiv1alpha1.ManagedClusterAddOn,
|
||||
csr *certificatesv1.CertificateSigningRequest) bool {
|
||||
|
||||
klog.Infof("Customer signer CSR is approved. cluster: %s, addon %s, requester: %s",
|
||||
cluster.Name, addon.Name, csr.Spec.Username)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func (a *CRDTemplateAgentAddon) TemplateCSRSignFunc() agent.CSRSignerFunc {
|
||||
|
||||
return func(csr *certificatesv1.CertificateSigningRequest) []byte {
|
||||
// TODO: consider to change the agent.CSRSignerFun to accept parameter addon
|
||||
getClusterName := func(userName string) string {
|
||||
return csr.Labels[clusterv1.ClusterNameLabelKey]
|
||||
}
|
||||
|
||||
clusterName := getClusterName(csr.Spec.Username)
|
||||
template, err := a.GetDesiredAddOnTemplate(nil, clusterName, a.addonName)
|
||||
if err != nil {
|
||||
utilruntime.HandleError(fmt.Errorf("failed to get template for addon %s in cluster %s: %v",
|
||||
a.addonName, clusterName, err))
|
||||
return nil
|
||||
}
|
||||
if template == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, registration := range template.Spec.Registration {
|
||||
switch registration.Type {
|
||||
case addonapiv1alpha1.RegistrationTypeKubeClient:
|
||||
continue
|
||||
|
||||
case addonapiv1alpha1.RegistrationTypeCustomSigner:
|
||||
if registration.CustomSigner == nil {
|
||||
continue
|
||||
}
|
||||
if csr.Spec.SignerName == registration.CustomSigner.SignerName {
|
||||
return CustomSignerWithExpiry(a.hubKubeClient, registration.CustomSigner, 24*time.Hour)(csr)
|
||||
}
|
||||
|
||||
default:
|
||||
utilruntime.HandleError(fmt.Errorf("unsupported registration type %s", registration.Type))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func CustomSignerWithExpiry(
|
||||
kubeclient kubernetes.Interface,
|
||||
customSignerConfig *addonapiv1alpha1.CustomSignerRegistrationConfig,
|
||||
duration time.Duration) agent.CSRSignerFunc {
|
||||
return func(csr *certificatesv1.CertificateSigningRequest) []byte {
|
||||
if customSignerConfig == nil {
|
||||
utilruntime.HandleError(fmt.Errorf("custome signer is nil"))
|
||||
return nil
|
||||
}
|
||||
|
||||
if csr.Spec.SignerName != customSignerConfig.SignerName {
|
||||
return nil
|
||||
}
|
||||
caSecret, err := kubeclient.CoreV1().Secrets(customSignerConfig.SigningCA.Namespace).Get(
|
||||
context.TODO(), customSignerConfig.SigningCA.Name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
utilruntime.HandleError(fmt.Errorf("get custome signer ca %s/%s failed, %v",
|
||||
customSignerConfig.SigningCA.Namespace, customSignerConfig.SigningCA.Name, err))
|
||||
return nil
|
||||
}
|
||||
|
||||
caData, caKey, err := extractCAdata(caSecret.Data[TLSCACert], caSecret.Data[TLSCAKey])
|
||||
if err != nil {
|
||||
utilruntime.HandleError(fmt.Errorf("get ca %s/%s data failed, %v",
|
||||
customSignerConfig.SigningCA.Namespace, customSignerConfig.SigningCA.Name, err))
|
||||
return nil
|
||||
}
|
||||
return utils.DefaultSignerWithExpiry(caKey, caData, duration)(csr)
|
||||
}
|
||||
}
|
||||
|
||||
func extractCAdata(caCertData, caKeyData []byte) ([]byte, []byte, error) {
|
||||
certBlock, _ := pem.Decode(caCertData)
|
||||
if certBlock == nil {
|
||||
return nil, nil, errors.New("failed to decode ca cert")
|
||||
}
|
||||
caCert, err := x509.ParseCertificate(certBlock.Bytes)
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrapf(err, "failed to parse ca certificate")
|
||||
}
|
||||
keyBlock, _ := pem.Decode(caKeyData)
|
||||
if keyBlock == nil {
|
||||
return nil, nil, errors.New("failed to decode ca key")
|
||||
}
|
||||
caKey, err := x509.ParsePKCS8PrivateKey(keyBlock.Bytes)
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrapf(err, "failed to parse ca key")
|
||||
}
|
||||
|
||||
caConfig := &openshiftcrypto.TLSCertificateConfig{
|
||||
Certs: []*x509.Certificate{caCert},
|
||||
Key: caKey,
|
||||
}
|
||||
return caConfig.GetPEMBytes()
|
||||
}
|
||||
|
||||
// TemplatePermissionConfigFunc returns a func that can grant permission for addon agent
|
||||
// that is deployed by addon template.
|
||||
// the returned func will create a rolebinding to bind the clusterRole/role which is
|
||||
// specified by the user, so the user is required to make sure the existence of the
|
||||
// clusterRole/role
|
||||
func (a *CRDTemplateAgentAddon) TemplatePermissionConfigFunc() agent.PermissionConfigFunc {
|
||||
|
||||
return func(cluster *clusterv1.ManagedCluster, addon *addonapiv1alpha1.ManagedClusterAddOn) error {
|
||||
template, err := a.GetDesiredAddOnTemplate(addon, cluster.Name, a.addonName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if template == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, registration := range template.Spec.Registration {
|
||||
switch registration.Type {
|
||||
case addonapiv1alpha1.RegistrationTypeKubeClient:
|
||||
kcrc := registration.KubeClient
|
||||
if kcrc == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
err := a.createKubeClientPermissions(template.Name, kcrc, cluster, addon)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
case addonapiv1alpha1.RegistrationTypeCustomSigner:
|
||||
continue
|
||||
|
||||
default:
|
||||
utilruntime.HandleError(fmt.Errorf("unsupported registration type %s", registration.Type))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *CRDTemplateAgentAddon) createKubeClientPermissions(
|
||||
templateName string,
|
||||
kcrc *addonapiv1alpha1.KubeClientRegistrationConfig,
|
||||
cluster *clusterv1.ManagedCluster,
|
||||
addon *addonapiv1alpha1.ManagedClusterAddOn,
|
||||
) error {
|
||||
|
||||
for _, pc := range kcrc.HubPermissions {
|
||||
switch pc.Type {
|
||||
case addonapiv1alpha1.HubPermissionsBindingCurrentCluster:
|
||||
klog.V(5).Infof("Set hub permission for addon %s/%s, UID: %s, APIVersion: %s, Kind: %s",
|
||||
addon.Namespace, addon.Name, addon.UID, addon.APIVersion, addon.Kind)
|
||||
|
||||
owner := metav1.OwnerReference{
|
||||
// TODO: use apiVersion and kind in addon object, but now they could be empty at some unknown reason
|
||||
APIVersion: "addon.open-cluster-management.io/v1alpha1",
|
||||
Kind: "ManagedClusterAddOn",
|
||||
Name: addon.Name,
|
||||
UID: addon.UID,
|
||||
}
|
||||
err := a.createPermissionBinding(templateName,
|
||||
cluster.Name, addon.Name, cluster.Name, pc.RoleRef, &owner)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case addonapiv1alpha1.HubPermissionsBindingSingleNamespace:
|
||||
if pc.SingleNamespace == nil {
|
||||
return fmt.Errorf("single namespace is nil")
|
||||
}
|
||||
// set owner reference nil since the rolebinding has different namespace with the ManagedClusterAddon
|
||||
// TODO: cleanup the rolebinding when the addon is deleted
|
||||
err := a.createPermissionBinding(templateName,
|
||||
cluster.Name, addon.Name, pc.SingleNamespace.Namespace, pc.RoleRef, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *CRDTemplateAgentAddon) createPermissionBinding(templateName, clusterName, addonName, namespace string,
|
||||
roleRef rbacv1.RoleRef, owner *metav1.OwnerReference) error {
|
||||
// TODO: confirm the group
|
||||
groups := agent.DefaultGroups(clusterName, addonName)
|
||||
subject := []rbacv1.Subject{}
|
||||
for _, group := range groups {
|
||||
subject = append(subject, rbacv1.Subject{
|
||||
Kind: "Group", APIGroup: "rbac.authorization.k8s.io", Name: group,
|
||||
})
|
||||
}
|
||||
binding := &rbacv1.RoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: fmt.Sprintf("open-cluster-management:%s:%s:agent",
|
||||
addonName, strings.ToLower(roleRef.Kind)),
|
||||
Namespace: namespace,
|
||||
Labels: map[string]string{
|
||||
addonapiv1alpha1.AddonLabelKey: addonName,
|
||||
AddonTemplateLabelKey: "",
|
||||
},
|
||||
},
|
||||
RoleRef: roleRef,
|
||||
Subjects: subject,
|
||||
}
|
||||
if owner != nil {
|
||||
binding.OwnerReferences = []metav1.OwnerReference{*owner}
|
||||
}
|
||||
_, err := a.rolebindingLister.RoleBindings(namespace).Get(binding.Name)
|
||||
switch {
|
||||
case err == nil:
|
||||
// TODO: update the rolebinding if it is not the same
|
||||
klog.Infof("rolebinding %s already exists", binding.Name)
|
||||
return nil
|
||||
case apierrors.IsNotFound(err):
|
||||
_, createErr := a.hubKubeClient.RbacV1().RoleBindings(namespace).Create(
|
||||
context.TODO(), binding, metav1.CreateOptions{})
|
||||
if createErr != nil && !apierrors.IsAlreadyExists(createErr) {
|
||||
return createErr
|
||||
}
|
||||
case err != nil:
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
611
pkg/addon/templateagent/registration_test.go
Normal file
611
pkg/addon/templateagent/registration_test.go
Normal file
@@ -0,0 +1,611 @@
|
||||
package templateagent
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
certificatesv1 "k8s.io/api/certificates/v1"
|
||||
rbacv1 "k8s.io/api/rbac/v1"
|
||||
"k8s.io/apimachinery/pkg/api/equality"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
kubeinformers "k8s.io/client-go/informers"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
fakekube "k8s.io/client-go/kubernetes/fake"
|
||||
|
||||
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
fakeaddon "open-cluster-management.io/api/client/addon/clientset/versioned/fake"
|
||||
addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions"
|
||||
clusterv1 "open-cluster-management.io/api/cluster/v1"
|
||||
)
|
||||
|
||||
func TestTemplateCSRConfigurationsFunc(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
agentName string
|
||||
cluster *clusterv1.ManagedCluster
|
||||
addon *addonapiv1alpha1.ManagedClusterAddOn
|
||||
template *addonapiv1alpha1.AddOnTemplate
|
||||
expectedConfigs []addonapiv1alpha1.RegistrationConfig
|
||||
}{
|
||||
{
|
||||
name: "empty",
|
||||
agentName: "agent1",
|
||||
cluster: NewFakeManagedCluster("cluster1"),
|
||||
addon: NewFakeTemplateManagedClusterAddon("addon1", "cluster1", "", ""),
|
||||
template: NewFakeAddonTemplate("template1", []addonapiv1alpha1.RegistrationSpec{}),
|
||||
expectedConfigs: []addonapiv1alpha1.RegistrationConfig{},
|
||||
},
|
||||
{
|
||||
name: "kubeclient",
|
||||
agentName: "agent1",
|
||||
cluster: NewFakeManagedCluster("cluster1"),
|
||||
template: NewFakeAddonTemplate("template1", []addonapiv1alpha1.RegistrationSpec{
|
||||
{
|
||||
Type: addonapiv1alpha1.RegistrationTypeKubeClient,
|
||||
KubeClient: &addonapiv1alpha1.KubeClientRegistrationConfig{
|
||||
HubPermissions: []addonapiv1alpha1.HubPermissionConfig{
|
||||
{
|
||||
Type: addonapiv1alpha1.HubPermissionsBindingSingleNamespace,
|
||||
RoleRef: rbacv1.RoleRef{
|
||||
APIGroup: "rbac.authorization.k8s.io",
|
||||
Kind: "ClusterRole",
|
||||
Name: "test",
|
||||
},
|
||||
SingleNamespace: &addonapiv1alpha1.SingleNamespaceBindingConfig{
|
||||
Namespace: "test",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
addon: NewFakeTemplateManagedClusterAddon("addon1", "cluster1", "template1", "fakehash"),
|
||||
expectedConfigs: []addonapiv1alpha1.RegistrationConfig{
|
||||
{
|
||||
SignerName: "kubernetes.io/kube-apiserver-client",
|
||||
Subject: addonapiv1alpha1.Subject{
|
||||
User: "system:open-cluster-management:cluster:cluster1:addon:addon1:agent:agent1",
|
||||
|
||||
Groups: []string{
|
||||
"system:open-cluster-management:cluster:cluster1:addon:addon1",
|
||||
"system:open-cluster-management:addon:addon1",
|
||||
"system:authenticated",
|
||||
},
|
||||
OrganizationUnits: []string{},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "customsigner",
|
||||
agentName: "agent1",
|
||||
cluster: NewFakeManagedCluster("cluster1"),
|
||||
template: NewFakeAddonTemplate("template1", []addonapiv1alpha1.RegistrationSpec{
|
||||
{
|
||||
Type: addonapiv1alpha1.RegistrationTypeCustomSigner,
|
||||
CustomSigner: &addonapiv1alpha1.CustomSignerRegistrationConfig{
|
||||
SignerName: "s1",
|
||||
Subject: &addonapiv1alpha1.Subject{
|
||||
User: "u1",
|
||||
Groups: []string{
|
||||
"g1",
|
||||
"g2",
|
||||
},
|
||||
OrganizationUnits: []string{},
|
||||
},
|
||||
SigningCA: addonapiv1alpha1.SigningCARef{
|
||||
Namespace: "ns1",
|
||||
Name: "name1"},
|
||||
},
|
||||
},
|
||||
}),
|
||||
addon: NewFakeTemplateManagedClusterAddon("addon1", "cluster1", "template1", "fakehash"),
|
||||
expectedConfigs: []addonapiv1alpha1.RegistrationConfig{
|
||||
{
|
||||
SignerName: "s1",
|
||||
Subject: addonapiv1alpha1.Subject{
|
||||
User: "u1",
|
||||
Groups: []string{
|
||||
"g1",
|
||||
"g2",
|
||||
},
|
||||
OrganizationUnits: []string{},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
addonClient := fakeaddon.NewSimpleClientset(c.template, c.addon)
|
||||
addonInformerFactory := addoninformers.NewSharedInformerFactory(addonClient, 30*time.Minute)
|
||||
mcaStore := addonInformerFactory.Addon().V1alpha1().ManagedClusterAddOns().Informer().GetStore()
|
||||
if err := mcaStore.Add(c.addon); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
atStore := addonInformerFactory.Addon().V1alpha1().AddOnTemplates().Informer().GetStore()
|
||||
if err := atStore.Add(c.template); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
agent := NewCRDTemplateAgentAddon(c.addon.Name, c.agentName, nil, addonClient, addonInformerFactory, nil, nil)
|
||||
f := agent.TemplateCSRConfigurationsFunc()
|
||||
registrationConfigs := f(c.cluster)
|
||||
if !equality.Semantic.DeepEqual(registrationConfigs, c.expectedConfigs) {
|
||||
t.Errorf("expected registrationConfigs %v, but got %v", c.expectedConfigs, registrationConfigs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTemplateCSRApproveCheckFunc(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
agentName string
|
||||
cluster *clusterv1.ManagedCluster
|
||||
addon *addonapiv1alpha1.ManagedClusterAddOn
|
||||
template *addonapiv1alpha1.AddOnTemplate
|
||||
csr *certificatesv1.CertificateSigningRequest
|
||||
expectedApprove bool
|
||||
}{
|
||||
{
|
||||
name: "empty",
|
||||
agentName: "agent1",
|
||||
cluster: NewFakeManagedCluster("cluster1"),
|
||||
addon: NewFakeTemplateManagedClusterAddon("addon1", "cluster1", "", ""),
|
||||
template: NewFakeAddonTemplate("template1", []addonapiv1alpha1.RegistrationSpec{}),
|
||||
expectedApprove: false,
|
||||
},
|
||||
{
|
||||
name: "kubeclient",
|
||||
agentName: "agent1",
|
||||
cluster: NewFakeManagedCluster("cluster1"),
|
||||
template: NewFakeAddonTemplate("template1", []addonapiv1alpha1.RegistrationSpec{
|
||||
{
|
||||
Type: addonapiv1alpha1.RegistrationTypeKubeClient,
|
||||
KubeClient: &addonapiv1alpha1.KubeClientRegistrationConfig{
|
||||
HubPermissions: []addonapiv1alpha1.HubPermissionConfig{
|
||||
{
|
||||
Type: addonapiv1alpha1.HubPermissionsBindingSingleNamespace,
|
||||
RoleRef: rbacv1.RoleRef{
|
||||
APIGroup: "rbac.authorization.k8s.io",
|
||||
Kind: "ClusterRole",
|
||||
Name: "test",
|
||||
},
|
||||
SingleNamespace: &addonapiv1alpha1.SingleNamespaceBindingConfig{
|
||||
Namespace: "test",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
addon: NewFakeTemplateManagedClusterAddon("addon1", "cluster1", "template1", "fakehash"),
|
||||
csr: &certificatesv1.CertificateSigningRequest{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "csr1",
|
||||
},
|
||||
Spec: certificatesv1.CertificateSigningRequestSpec{
|
||||
SignerName: "kubernetes.io/kube-apiserver-client",
|
||||
},
|
||||
},
|
||||
expectedApprove: false, // fake csr data
|
||||
},
|
||||
{
|
||||
name: "customsigner",
|
||||
agentName: "agent1",
|
||||
cluster: NewFakeManagedCluster("cluster1"),
|
||||
template: NewFakeAddonTemplate("template1", []addonapiv1alpha1.RegistrationSpec{
|
||||
{
|
||||
Type: addonapiv1alpha1.RegistrationTypeCustomSigner,
|
||||
CustomSigner: &addonapiv1alpha1.CustomSignerRegistrationConfig{
|
||||
SignerName: "s1",
|
||||
Subject: &addonapiv1alpha1.Subject{
|
||||
User: "u1",
|
||||
Groups: []string{
|
||||
"g1",
|
||||
"g2",
|
||||
},
|
||||
OrganizationUnits: []string{},
|
||||
},
|
||||
SigningCA: addonapiv1alpha1.SigningCARef{
|
||||
Namespace: "ns1",
|
||||
Name: "name1"},
|
||||
},
|
||||
},
|
||||
}),
|
||||
addon: NewFakeTemplateManagedClusterAddon("addon1", "cluster1", "template1", "fakehash"),
|
||||
csr: &certificatesv1.CertificateSigningRequest{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "csr1",
|
||||
},
|
||||
Spec: certificatesv1.CertificateSigningRequestSpec{
|
||||
SignerName: "s1",
|
||||
},
|
||||
},
|
||||
expectedApprove: true,
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
addonClient := fakeaddon.NewSimpleClientset(c.template, c.addon)
|
||||
addonInformerFactory := addoninformers.NewSharedInformerFactory(addonClient, 30*time.Minute)
|
||||
mcaStore := addonInformerFactory.Addon().V1alpha1().ManagedClusterAddOns().Informer().GetStore()
|
||||
if err := mcaStore.Add(c.addon); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
atStore := addonInformerFactory.Addon().V1alpha1().AddOnTemplates().Informer().GetStore()
|
||||
if err := atStore.Add(c.template); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
agent := NewCRDTemplateAgentAddon(c.addon.Name, c.agentName, nil, addonClient, addonInformerFactory, nil, nil)
|
||||
f := agent.TemplateCSRApproveCheckFunc()
|
||||
approve := f(c.cluster, c.addon, c.csr)
|
||||
if approve != c.expectedApprove {
|
||||
t.Errorf("expected approve result %v, but got %v", c.expectedApprove, approve)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTemplateCSRSignFunc(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
agentName string
|
||||
cluster *clusterv1.ManagedCluster
|
||||
addon *addonapiv1alpha1.ManagedClusterAddOn
|
||||
template *addonapiv1alpha1.AddOnTemplate
|
||||
csr *certificatesv1.CertificateSigningRequest
|
||||
expectedCert []byte
|
||||
}{
|
||||
{
|
||||
name: "kubeclient",
|
||||
agentName: "agent1",
|
||||
cluster: NewFakeManagedCluster("cluster1"),
|
||||
template: NewFakeAddonTemplate("template1", []addonapiv1alpha1.RegistrationSpec{
|
||||
{
|
||||
Type: addonapiv1alpha1.RegistrationTypeKubeClient,
|
||||
KubeClient: &addonapiv1alpha1.KubeClientRegistrationConfig{
|
||||
HubPermissions: []addonapiv1alpha1.HubPermissionConfig{
|
||||
{
|
||||
Type: addonapiv1alpha1.HubPermissionsBindingSingleNamespace,
|
||||
RoleRef: rbacv1.RoleRef{
|
||||
APIGroup: "rbac.authorization.k8s.io",
|
||||
Kind: "ClusterRole",
|
||||
Name: "test",
|
||||
},
|
||||
SingleNamespace: &addonapiv1alpha1.SingleNamespaceBindingConfig{
|
||||
Namespace: "test",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
addon: NewFakeTemplateManagedClusterAddon("addon1", "cluster1", "template1", "fakehash"),
|
||||
csr: &certificatesv1.CertificateSigningRequest{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "csr1",
|
||||
},
|
||||
Spec: certificatesv1.CertificateSigningRequestSpec{
|
||||
SignerName: "kubernetes.io/kube-apiserver-client",
|
||||
Username: "system:open-cluster-management:cluster1:adcde",
|
||||
},
|
||||
},
|
||||
expectedCert: nil,
|
||||
},
|
||||
{
|
||||
name: "customsigner no ca secret",
|
||||
agentName: "agent1",
|
||||
cluster: NewFakeManagedCluster("cluster1"),
|
||||
template: NewFakeAddonTemplate("template1", []addonapiv1alpha1.RegistrationSpec{
|
||||
{
|
||||
Type: addonapiv1alpha1.RegistrationTypeCustomSigner,
|
||||
CustomSigner: &addonapiv1alpha1.CustomSignerRegistrationConfig{
|
||||
SignerName: "s1",
|
||||
Subject: &addonapiv1alpha1.Subject{
|
||||
User: "u1",
|
||||
Groups: []string{
|
||||
"g1",
|
||||
"g2",
|
||||
},
|
||||
OrganizationUnits: []string{},
|
||||
},
|
||||
SigningCA: addonapiv1alpha1.SigningCARef{
|
||||
Namespace: "ns1",
|
||||
Name: "name1"},
|
||||
},
|
||||
},
|
||||
}),
|
||||
addon: NewFakeTemplateManagedClusterAddon("addon1", "cluster1", "template1", "fakehash"),
|
||||
csr: &certificatesv1.CertificateSigningRequest{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "csr1",
|
||||
},
|
||||
Spec: certificatesv1.CertificateSigningRequestSpec{
|
||||
SignerName: "s1",
|
||||
Username: "system:open-cluster-management:cluster1:adcde",
|
||||
},
|
||||
},
|
||||
expectedCert: nil,
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
addonClient := fakeaddon.NewSimpleClientset(c.template, c.addon)
|
||||
hubKubeClient := fakekube.NewSimpleClientset()
|
||||
addonInformerFactory := addoninformers.NewSharedInformerFactory(addonClient, 30*time.Minute)
|
||||
mcaStore := addonInformerFactory.Addon().V1alpha1().ManagedClusterAddOns().Informer().GetStore()
|
||||
if err := mcaStore.Add(c.addon); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
atStore := addonInformerFactory.Addon().V1alpha1().AddOnTemplates().Informer().GetStore()
|
||||
if err := atStore.Add(c.template); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
agent := NewCRDTemplateAgentAddon(c.addon.Name, c.agentName, hubKubeClient, addonClient, addonInformerFactory, nil, nil)
|
||||
f := agent.TemplateCSRSignFunc()
|
||||
cert := f(c.csr)
|
||||
if !bytes.Equal(cert, c.expectedCert) {
|
||||
t.Errorf("expected cert %v, but got %v", c.expectedCert, cert)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewFakeManagedCluster(name string) *clusterv1.ManagedCluster {
|
||||
return &clusterv1.ManagedCluster{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
Kind: "ManagedCluster",
|
||||
APIVersion: clusterv1.SchemeGroupVersion.String(),
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
},
|
||||
Spec: clusterv1.ManagedClusterSpec{},
|
||||
}
|
||||
}
|
||||
|
||||
func NewFakeTemplateManagedClusterAddon(name, clusterName, addonTemplateName, addonTemplateSpecHash string) *addonapiv1alpha1.ManagedClusterAddOn {
|
||||
addon := &addonapiv1alpha1.ManagedClusterAddOn{
|
||||
TypeMeta: metav1.TypeMeta{},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Namespace: clusterName,
|
||||
},
|
||||
Spec: addonapiv1alpha1.ManagedClusterAddOnSpec{},
|
||||
Status: addonapiv1alpha1.ManagedClusterAddOnStatus{},
|
||||
}
|
||||
|
||||
if addonTemplateName != "" {
|
||||
addon.Status.ConfigReferences = []addonapiv1alpha1.ConfigReference{
|
||||
{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: "addon.open-cluster-management.io",
|
||||
Resource: "addontemplates",
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Name: addonTemplateName,
|
||||
},
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Name: addonTemplateName,
|
||||
},
|
||||
SpecHash: addonTemplateSpecHash,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
return addon
|
||||
}
|
||||
|
||||
func NewFakeAddonTemplate(name string,
|
||||
registrationSpec []addonapiv1alpha1.RegistrationSpec) *addonapiv1alpha1.AddOnTemplate {
|
||||
return &addonapiv1alpha1.AddOnTemplate{
|
||||
TypeMeta: metav1.TypeMeta{},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
},
|
||||
Spec: addonapiv1alpha1.AddOnTemplateSpec{
|
||||
Registration: registrationSpec,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func NewFakeRoleBinding(addonName, namespace string, subject []rbacv1.Subject, roleRef rbacv1.RoleRef,
|
||||
owner metav1.OwnerReference) *rbacv1.RoleBinding {
|
||||
return &rbacv1.RoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: fmt.Sprintf("open-cluster-management:%s:%s:agent",
|
||||
addonName, strings.ToLower(roleRef.Kind)),
|
||||
Namespace: namespace,
|
||||
OwnerReferences: []metav1.OwnerReference{owner},
|
||||
Labels: map[string]string{
|
||||
addonapiv1alpha1.AddonLabelKey: addonName,
|
||||
},
|
||||
},
|
||||
RoleRef: roleRef,
|
||||
Subjects: subject,
|
||||
}
|
||||
}
|
||||
|
||||
func TestTemplatePermissionConfigFunc(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
agentName string
|
||||
cluster *clusterv1.ManagedCluster
|
||||
addon *addonapiv1alpha1.ManagedClusterAddOn
|
||||
template *addonapiv1alpha1.AddOnTemplate
|
||||
rolebinding *rbacv1.RoleBinding
|
||||
expectedErr error
|
||||
validatePermissionFunc func(*testing.T, kubernetes.Interface)
|
||||
}{
|
||||
{
|
||||
name: "kubeclient current cluster binding",
|
||||
agentName: "agent1",
|
||||
cluster: NewFakeManagedCluster("cluster1"),
|
||||
template: NewFakeAddonTemplate("template1", []addonapiv1alpha1.RegistrationSpec{
|
||||
{
|
||||
Type: addonapiv1alpha1.RegistrationTypeKubeClient,
|
||||
KubeClient: &addonapiv1alpha1.KubeClientRegistrationConfig{
|
||||
HubPermissions: []addonapiv1alpha1.HubPermissionConfig{
|
||||
{
|
||||
Type: addonapiv1alpha1.HubPermissionsBindingCurrentCluster,
|
||||
RoleRef: rbacv1.RoleRef{
|
||||
APIGroup: "rbac.authorization.k8s.io",
|
||||
Kind: "Role",
|
||||
Name: "test",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
addon: NewFakeTemplateManagedClusterAddon("addon1", "cluster1", "template1", "fakehash"),
|
||||
rolebinding: NewFakeRoleBinding("addon1", "cluster1",
|
||||
[]rbacv1.Subject{{
|
||||
Kind: "Group",
|
||||
APIGroup: "rbac.authorization.k8s.io",
|
||||
Name: "system:authenticated"},
|
||||
}, rbacv1.RoleRef{
|
||||
APIGroup: "rbac.authorization.k8s.io",
|
||||
Kind: "Role",
|
||||
Name: "test",
|
||||
},
|
||||
metav1.OwnerReference{
|
||||
APIVersion: "addon.open-cluster-management.io/v1alpha1",
|
||||
Kind: "ManagedClusterAddOn",
|
||||
Name: "addon1",
|
||||
UID: "fakeuid",
|
||||
}),
|
||||
expectedErr: nil,
|
||||
validatePermissionFunc: func(t *testing.T, kubeClient kubernetes.Interface) {
|
||||
rb, err := kubeClient.RbacV1().RoleBindings("cluster1").Get(context.TODO(),
|
||||
fmt.Sprintf("open-cluster-management:%s:%s:agent", "addon1", strings.ToLower("Role")),
|
||||
metav1.GetOptions{},
|
||||
)
|
||||
if err != nil {
|
||||
t.Errorf("failed to get rolebinding: %v", err)
|
||||
}
|
||||
|
||||
if rb.RoleRef.Name != "test" {
|
||||
t.Errorf("expected rolebinding %s, got %s", "test", rb.RoleRef.Name)
|
||||
}
|
||||
if len(rb.OwnerReferences) != 1 {
|
||||
t.Errorf("expected rolebinding to have 1 owner reference, got %d", len(rb.OwnerReferences))
|
||||
}
|
||||
if rb.OwnerReferences[0].Kind != "ManagedClusterAddOn" {
|
||||
t.Errorf("expected rolebinding owner reference kind to be ManagedClusterAddOn, got %s",
|
||||
rb.OwnerReferences[0].Kind)
|
||||
}
|
||||
if rb.OwnerReferences[0].Name != "addon1" {
|
||||
t.Errorf("expected rolebinding owner reference name to be addon1, got %s",
|
||||
rb.OwnerReferences[0].Name)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "kubeclient single namespace binding",
|
||||
agentName: "agent1",
|
||||
cluster: NewFakeManagedCluster("cluster1"),
|
||||
template: NewFakeAddonTemplate("template1", []addonapiv1alpha1.RegistrationSpec{
|
||||
{
|
||||
Type: addonapiv1alpha1.RegistrationTypeKubeClient,
|
||||
KubeClient: &addonapiv1alpha1.KubeClientRegistrationConfig{
|
||||
HubPermissions: []addonapiv1alpha1.HubPermissionConfig{
|
||||
{
|
||||
Type: addonapiv1alpha1.HubPermissionsBindingSingleNamespace,
|
||||
RoleRef: rbacv1.RoleRef{
|
||||
APIGroup: "rbac.authorization.k8s.io",
|
||||
Kind: "ClusterRole",
|
||||
Name: "test",
|
||||
},
|
||||
SingleNamespace: &addonapiv1alpha1.SingleNamespaceBindingConfig{
|
||||
Namespace: "test",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
addon: NewFakeTemplateManagedClusterAddon("addon1", "cluster1", "template1", "fakehash"),
|
||||
expectedErr: nil,
|
||||
validatePermissionFunc: func(t *testing.T, kubeClient kubernetes.Interface) {
|
||||
rb, err := kubeClient.RbacV1().RoleBindings("test").Get(context.TODO(),
|
||||
fmt.Sprintf("open-cluster-management:%s:%s:agent", "addon1", strings.ToLower("ClusterRole")),
|
||||
metav1.GetOptions{},
|
||||
)
|
||||
if err != nil {
|
||||
t.Errorf("failed to get rolebinding: %v", err)
|
||||
}
|
||||
|
||||
if rb.RoleRef.Name != "test" {
|
||||
t.Errorf("expected rolebinding %s, got %s", "test", rb.RoleRef.Name)
|
||||
}
|
||||
if len(rb.OwnerReferences) != 0 {
|
||||
t.Errorf("expected rolebinding to have 0 owner reference, got %d", len(rb.OwnerReferences))
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "customsigner",
|
||||
agentName: "agent1",
|
||||
cluster: NewFakeManagedCluster("cluster1"),
|
||||
template: NewFakeAddonTemplate("template1", []addonapiv1alpha1.RegistrationSpec{
|
||||
{
|
||||
Type: addonapiv1alpha1.RegistrationTypeCustomSigner,
|
||||
CustomSigner: &addonapiv1alpha1.CustomSignerRegistrationConfig{
|
||||
SignerName: "s1",
|
||||
Subject: &addonapiv1alpha1.Subject{
|
||||
User: "u1",
|
||||
Groups: []string{
|
||||
"g1",
|
||||
"g2",
|
||||
},
|
||||
OrganizationUnits: []string{},
|
||||
},
|
||||
SigningCA: addonapiv1alpha1.SigningCARef{
|
||||
Namespace: "ns1",
|
||||
Name: "name1"},
|
||||
},
|
||||
},
|
||||
}),
|
||||
addon: NewFakeTemplateManagedClusterAddon("addon1", "cluster1", "template1", "fakehash"),
|
||||
expectedErr: nil,
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
addonClient := fakeaddon.NewSimpleClientset(c.template, c.addon)
|
||||
hubKubeClient := fakekube.NewSimpleClientset()
|
||||
if c.rolebinding != nil {
|
||||
hubKubeClient = fakekube.NewSimpleClientset(c.rolebinding)
|
||||
}
|
||||
addonInformerFactory := addoninformers.NewSharedInformerFactory(addonClient, 30*time.Minute)
|
||||
mcaStore := addonInformerFactory.Addon().V1alpha1().ManagedClusterAddOns().Informer().GetStore()
|
||||
if err := mcaStore.Add(c.addon); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
atStore := addonInformerFactory.Addon().V1alpha1().AddOnTemplates().Informer().GetStore()
|
||||
if err := atStore.Add(c.template); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
kubeInformers := kubeinformers.NewSharedInformerFactoryWithOptions(hubKubeClient, 10*time.Minute)
|
||||
if c.rolebinding != nil {
|
||||
|
||||
rbStore := kubeInformers.Rbac().V1().RoleBindings().Informer().GetStore()
|
||||
if err := rbStore.Add(c.rolebinding); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
agent := NewCRDTemplateAgentAddon(c.addon.Name, c.agentName, hubKubeClient, addonClient, addonInformerFactory,
|
||||
kubeInformers.Rbac().V1().RoleBindings().Lister(), nil)
|
||||
f := agent.TemplatePermissionConfigFunc()
|
||||
err := f(c.cluster, c.addon)
|
||||
if err != c.expectedErr {
|
||||
t.Errorf("expected registrationConfigs %v, but got %v", c.expectedErr, err)
|
||||
}
|
||||
if c.validatePermissionFunc != nil {
|
||||
c.validatePermissionFunc(t, hubKubeClient)
|
||||
}
|
||||
}
|
||||
}
|
||||
220
pkg/addon/templateagent/template_agent.go
Normal file
220
pkg/addon/templateagent/template_agent.go
Normal file
@@ -0,0 +1,220 @@
|
||||
package templateagent
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/valyala/fasttemplate"
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
rbacv1lister "k8s.io/client-go/listers/rbac/v1"
|
||||
"k8s.io/klog/v2"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/addonfactory"
|
||||
"open-cluster-management.io/addon-framework/pkg/agent"
|
||||
"open-cluster-management.io/addon-framework/pkg/utils"
|
||||
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
addonv1alpha1client "open-cluster-management.io/api/client/addon/clientset/versioned"
|
||||
addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions"
|
||||
addonlisterv1alpha1 "open-cluster-management.io/api/client/addon/listers/addon/v1alpha1"
|
||||
clusterv1 "open-cluster-management.io/api/cluster/v1"
|
||||
)
|
||||
|
||||
const (
|
||||
NodePlacementPrivateValueKey = "__NODE_PLACEMENT"
|
||||
RegistriesPrivateValueKey = "__REGISTRIES"
|
||||
)
|
||||
|
||||
// templateBuiltinValues includes the built-in values for crd template agentAddon.
|
||||
// the values for template config should begin with an uppercase letter, so we need
|
||||
// to convert it to Values by JsonStructToValues.
|
||||
// the built-in values can not be overridden by getValuesFuncs
|
||||
type templateCRDBuiltinValues struct {
|
||||
ClusterName string `json:"CLUSTER_NAME,omitempty"`
|
||||
AddonInstallNamespace string `json:"INSTALL_NAMESPACE,omitempty"`
|
||||
}
|
||||
|
||||
// templateDefaultValues includes the default values for crd template agentAddon.
|
||||
// the values for template config should begin with an uppercase letter, so we need
|
||||
// to convert it to Values by JsonStructToValues.
|
||||
// the default values can be overridden by getValuesFuncs
|
||||
type templateCRDDefaultValues struct {
|
||||
HubKubeConfigPath string `json:"HUB_KUBECONFIG,omitempty"`
|
||||
ManagedKubeConfigPath string `json:"MANAGED_KUBECONFIG,omitempty"`
|
||||
}
|
||||
|
||||
type CRDTemplateAgentAddon struct {
|
||||
getValuesFuncs []addonfactory.GetValuesFunc
|
||||
trimCRDDescription bool
|
||||
|
||||
hubKubeClient kubernetes.Interface
|
||||
addonClient addonv1alpha1client.Interface
|
||||
addonLister addonlisterv1alpha1.ManagedClusterAddOnLister
|
||||
addonTemplateLister addonlisterv1alpha1.AddOnTemplateLister
|
||||
rolebindingLister rbacv1lister.RoleBindingLister
|
||||
addonName string
|
||||
agentName string
|
||||
}
|
||||
|
||||
// NewCRDTemplateAgentAddon creates a CRDTemplateAgentAddon instance
|
||||
func NewCRDTemplateAgentAddon(
|
||||
addonName, agentName string,
|
||||
hubKubeClient kubernetes.Interface,
|
||||
addonClient addonv1alpha1client.Interface,
|
||||
addonInformers addoninformers.SharedInformerFactory,
|
||||
rolebindingLister rbacv1lister.RoleBindingLister,
|
||||
getValuesFuncs ...addonfactory.GetValuesFunc,
|
||||
) *CRDTemplateAgentAddon {
|
||||
|
||||
a := &CRDTemplateAgentAddon{
|
||||
getValuesFuncs: getValuesFuncs,
|
||||
trimCRDDescription: true,
|
||||
|
||||
hubKubeClient: hubKubeClient,
|
||||
addonClient: addonClient,
|
||||
addonLister: addonInformers.Addon().V1alpha1().ManagedClusterAddOns().Lister(),
|
||||
addonTemplateLister: addonInformers.Addon().V1alpha1().AddOnTemplates().Lister(),
|
||||
rolebindingLister: rolebindingLister,
|
||||
addonName: addonName,
|
||||
agentName: agentName,
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *CRDTemplateAgentAddon) Manifests(
|
||||
cluster *clusterv1.ManagedCluster,
|
||||
addon *addonapiv1alpha1.ManagedClusterAddOn) ([]runtime.Object, error) {
|
||||
|
||||
template, err := a.GetDesiredAddOnTemplateByAddon(addon)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if template == nil {
|
||||
return nil, fmt.Errorf("addon %s/%s template not found in status", addon.Namespace, addon.Name)
|
||||
}
|
||||
return a.renderObjects(cluster, addon, template)
|
||||
}
|
||||
|
||||
func (a *CRDTemplateAgentAddon) GetAgentAddonOptions() agent.AgentAddonOptions {
|
||||
// TODO: consider a new way for developers to define their supported config GVRs
|
||||
supportedConfigGVRs := []schema.GroupVersionResource{}
|
||||
for gvr := range utils.BuiltInAddOnConfigGVRs {
|
||||
supportedConfigGVRs = append(supportedConfigGVRs, gvr)
|
||||
}
|
||||
return agent.AgentAddonOptions{
|
||||
AddonName: a.addonName,
|
||||
InstallStrategy: nil,
|
||||
HealthProber: nil,
|
||||
SupportedConfigGVRs: supportedConfigGVRs,
|
||||
Registration: &agent.RegistrationOption{
|
||||
CSRConfigurations: a.TemplateCSRConfigurationsFunc(),
|
||||
PermissionConfig: a.TemplatePermissionConfigFunc(),
|
||||
CSRApproveCheck: a.TemplateCSRApproveCheckFunc(),
|
||||
CSRSign: a.TemplateCSRSignFunc(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (a *CRDTemplateAgentAddon) renderObjects(
|
||||
cluster *clusterv1.ManagedCluster,
|
||||
addon *addonapiv1alpha1.ManagedClusterAddOn,
|
||||
template *addonapiv1alpha1.AddOnTemplate) ([]runtime.Object, error) {
|
||||
var objects []runtime.Object
|
||||
presetValues, configValues, privateValues, err := a.getValues(cluster, addon, template)
|
||||
if err != nil {
|
||||
return objects, err
|
||||
}
|
||||
klog.V(4).Infof("presetValues %v\t configValues: %v\t privateValues: %v", presetValues, configValues, privateValues)
|
||||
|
||||
for _, manifest := range template.Spec.AgentSpec.Workload.Manifests {
|
||||
t := fasttemplate.New(string(manifest.Raw), "{{", "}}")
|
||||
manifestStr := t.ExecuteString(configValues)
|
||||
klog.V(4).Infof("addon %s/%s render result: %v", addon.Namespace, addon.Name, manifestStr)
|
||||
object := &unstructured.Unstructured{}
|
||||
if err := object.UnmarshalJSON([]byte(manifestStr)); err != nil {
|
||||
return objects, err
|
||||
}
|
||||
objects = append(objects, object)
|
||||
}
|
||||
|
||||
objects, err = a.decorateObjects(template, objects, presetValues, configValues, privateValues)
|
||||
if err != nil {
|
||||
return objects, err
|
||||
}
|
||||
return objects, nil
|
||||
}
|
||||
|
||||
func (a *CRDTemplateAgentAddon) decorateObjects(
|
||||
template *addonapiv1alpha1.AddOnTemplate,
|
||||
objects []runtime.Object,
|
||||
orderedValues orderedValues,
|
||||
configValues, privateValues addonfactory.Values) ([]runtime.Object, error) {
|
||||
decorators := []deploymentDecorator{
|
||||
newEnvironmentDecorator(orderedValues),
|
||||
newVolumeDecorator(a.addonName, template),
|
||||
newNodePlacementDecorator(privateValues),
|
||||
newImageDecorator(privateValues),
|
||||
}
|
||||
for index, obj := range objects {
|
||||
deployment, err := a.convertToDeployment(obj)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, decorator := range decorators {
|
||||
err = decorator.decorate(deployment)
|
||||
if err != nil {
|
||||
return objects, err
|
||||
}
|
||||
}
|
||||
objects[index] = deployment
|
||||
}
|
||||
|
||||
return objects, nil
|
||||
}
|
||||
|
||||
func (a *CRDTemplateAgentAddon) convertToDeployment(obj runtime.Object) (*appsv1.Deployment, error) {
|
||||
if obj.GetObjectKind().GroupVersionKind().Group != "apps" ||
|
||||
obj.GetObjectKind().GroupVersionKind().Kind != "Deployment" {
|
||||
return nil, fmt.Errorf("not deployment object, %v", obj.GetObjectKind())
|
||||
}
|
||||
|
||||
deployment := &appsv1.Deployment{}
|
||||
uobj, ok := obj.(*unstructured.Unstructured)
|
||||
if !ok {
|
||||
return deployment, fmt.Errorf("not unstructured object, %v", obj.GetObjectKind())
|
||||
}
|
||||
|
||||
err := runtime.DefaultUnstructuredConverter.
|
||||
FromUnstructured(uobj.Object, deployment)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return deployment, nil
|
||||
}
|
||||
|
||||
// GetDesiredAddOnTemplateByAddon returns the desired template of the addon
|
||||
func (a *CRDTemplateAgentAddon) GetDesiredAddOnTemplateByAddon(
|
||||
addon *addonapiv1alpha1.ManagedClusterAddOn) (*addonapiv1alpha1.AddOnTemplate, error) {
|
||||
ok, templateRef := AddonTemplateConfigRef(addon.Status.ConfigReferences)
|
||||
if !ok {
|
||||
klog.V(4).Infof("Addon %s template config in status is empty", addon.Name)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
desiredTemplate := templateRef.DesiredConfig
|
||||
if desiredTemplate == nil || desiredTemplate.SpecHash == "" {
|
||||
klog.Infof("Addon %s template spec hash is empty", addon.Name)
|
||||
return nil, fmt.Errorf("addon %s template desired spec hash is empty", addon.Name)
|
||||
}
|
||||
|
||||
template, err := a.addonTemplateLister.Get(desiredTemplate.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return template.DeepCopy(), nil
|
||||
}
|
||||
232
pkg/addon/templateagent/template_agent_test.go
Normal file
232
pkg/addon/templateagent/template_agent_test.go
Normal file
@@ -0,0 +1,232 @@
|
||||
package templateagent
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/equality"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/serializer"
|
||||
kubeinformers "k8s.io/client-go/informers"
|
||||
fakekube "k8s.io/client-go/kubernetes/fake"
|
||||
"k8s.io/client-go/kubernetes/scheme"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/addonfactory"
|
||||
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
fakeaddon "open-cluster-management.io/api/client/addon/clientset/versioned/fake"
|
||||
addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions"
|
||||
clusterv1apha1 "open-cluster-management.io/api/cluster/v1alpha1"
|
||||
)
|
||||
|
||||
func TestAddonTemplateAgent_Manifests(t *testing.T) {
|
||||
addonName := "hello"
|
||||
clusterName := "cluster1"
|
||||
data, err := os.ReadFile("./testmanifests/addontemplate.yaml")
|
||||
if err != nil {
|
||||
t.Errorf("error reading file: %v", err)
|
||||
}
|
||||
|
||||
s := runtime.NewScheme()
|
||||
_ = scheme.AddToScheme(s)
|
||||
_ = clusterv1apha1.Install(s)
|
||||
_ = addonapiv1alpha1.Install(s)
|
||||
|
||||
addonTemplate := &addonapiv1alpha1.AddOnTemplate{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "hello-template",
|
||||
},
|
||||
}
|
||||
|
||||
addonTemplateSpecHash, err := GetTemplateSpecHash(addonTemplate)
|
||||
if err != nil {
|
||||
t.Errorf("error getting template spec hash: %v", err)
|
||||
}
|
||||
addonDeploymentConfig := &addonapiv1alpha1.AddOnDeploymentConfig{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "hello-config",
|
||||
Namespace: "default",
|
||||
},
|
||||
Spec: addonapiv1alpha1.AddOnDeploymentConfigSpec{
|
||||
CustomizedVariables: []addonapiv1alpha1.CustomizedVariable{
|
||||
{
|
||||
Name: "LOG_LEVEL",
|
||||
Value: "4",
|
||||
},
|
||||
},
|
||||
NodePlacement: &addonapiv1alpha1.NodePlacement{
|
||||
NodeSelector: map[string]string{
|
||||
"host": "ssd",
|
||||
},
|
||||
Tolerations: []corev1.Toleration{
|
||||
{
|
||||
Key: "foo",
|
||||
Operator: corev1.TolerationOpExists,
|
||||
Effect: corev1.TaintEffectNoExecute,
|
||||
},
|
||||
},
|
||||
},
|
||||
Registries: []addonapiv1alpha1.ImageMirror{
|
||||
{
|
||||
Source: "quay.io/open-cluster-management",
|
||||
Mirror: "quay.io/ocm",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
managedClusterAddon := &addonapiv1alpha1.ManagedClusterAddOn{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: addonName,
|
||||
Namespace: clusterName,
|
||||
},
|
||||
Status: addonapiv1alpha1.ManagedClusterAddOnStatus{
|
||||
ConfigReferences: []addonapiv1alpha1.ConfigReference{
|
||||
{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: "addon.open-cluster-management.io",
|
||||
Resource: "addontemplates",
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Name: "hello-template",
|
||||
},
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Name: "hello-template",
|
||||
},
|
||||
SpecHash: addonTemplateSpecHash,
|
||||
},
|
||||
},
|
||||
{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: "addon.open-cluster-management.io",
|
||||
Resource: "addondeploymentconfigs",
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Name: "hello-config",
|
||||
Namespace: "default",
|
||||
},
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Name: "hello-config",
|
||||
Namespace: "default",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
decoder := serializer.NewCodecFactory(s).UniversalDeserializer()
|
||||
_, _, err = decoder.Decode(data, nil, addonTemplate)
|
||||
if err != nil {
|
||||
t.Errorf("error decoding file: %v", err)
|
||||
}
|
||||
|
||||
hubKubeClient := fakekube.NewSimpleClientset()
|
||||
addonClient := fakeaddon.NewSimpleClientset(addonTemplate, managedClusterAddon, addonDeploymentConfig)
|
||||
addonInformerFactory := addoninformers.NewSharedInformerFactory(addonClient, 30*time.Minute)
|
||||
mcaStore := addonInformerFactory.Addon().V1alpha1().ManagedClusterAddOns().Informer().GetStore()
|
||||
if err := mcaStore.Add(managedClusterAddon); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
atStore := addonInformerFactory.Addon().V1alpha1().AddOnTemplates().Informer().GetStore()
|
||||
if err := atStore.Add(addonTemplate); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
kubeInformers := kubeinformers.NewSharedInformerFactoryWithOptions(hubKubeClient, 10*time.Minute)
|
||||
|
||||
agentAddon := NewCRDTemplateAgentAddon(
|
||||
addonName,
|
||||
"test-agent",
|
||||
hubKubeClient,
|
||||
addonClient,
|
||||
addonInformerFactory,
|
||||
kubeInformers.Rbac().V1().RoleBindings().Lister(),
|
||||
addonfactory.GetAddOnDeploymentConfigValues(
|
||||
addonfactory.NewAddOnDeploymentConfigGetter(addonClient),
|
||||
addonfactory.ToAddOnCustomizedVariableValues,
|
||||
ToAddOnNodePlacementPrivateValues,
|
||||
ToAddOnRegistriesPrivateValues,
|
||||
),
|
||||
)
|
||||
|
||||
cluster := addonfactory.NewFakeManagedCluster("cluster1", "1.10.1")
|
||||
|
||||
objects, err := agentAddon.Manifests(cluster, managedClusterAddon)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got err %v", err)
|
||||
}
|
||||
if len(objects) != 4 {
|
||||
t.Errorf("expected 4 objects, but got %v", len(objects))
|
||||
}
|
||||
|
||||
object, ok := objects[0].(*appsv1.Deployment)
|
||||
if !ok {
|
||||
t.Errorf("expected object to be *appsv1.Deployment, but got %T", objects[0])
|
||||
}
|
||||
|
||||
nodeSelector := object.Spec.Template.Spec.NodeSelector
|
||||
expectedNodeSelector := map[string]string{"host": "ssd"}
|
||||
if !equality.Semantic.DeepEqual(nodeSelector, expectedNodeSelector) {
|
||||
t.Errorf("unexpected nodeSelector %v", nodeSelector)
|
||||
}
|
||||
|
||||
tolerations := object.Spec.Template.Spec.Tolerations
|
||||
expectedTolerations := []corev1.Toleration{{Key: "foo", Operator: corev1.TolerationOpExists, Effect: corev1.TaintEffectNoExecute}}
|
||||
if !equality.Semantic.DeepEqual(tolerations, expectedTolerations) {
|
||||
t.Errorf("unexpected tolerations %v", tolerations)
|
||||
}
|
||||
|
||||
envs := object.Spec.Template.Spec.Containers[0].Env
|
||||
expectedEnvs := []corev1.EnvVar{
|
||||
{Name: "LOG_LEVEL", Value: "4"},
|
||||
{Name: "HUB_KUBECONFIG", Value: "/managed/hub-kubeconfig/kubeconfig"},
|
||||
{Name: "CLUSTER_NAME", Value: clusterName},
|
||||
{Name: "INSTALL_NAMESPACE", Value: "open-cluster-management-agent-addon"},
|
||||
}
|
||||
if !equality.Semantic.DeepEqual(envs, expectedEnvs) {
|
||||
t.Errorf("unexpected envs %v", envs)
|
||||
}
|
||||
|
||||
volumes := object.Spec.Template.Spec.Volumes
|
||||
expectedVolumes := []corev1.Volume{
|
||||
{
|
||||
Name: "hub-kubeconfig",
|
||||
VolumeSource: corev1.VolumeSource{
|
||||
Secret: &corev1.SecretVolumeSource{
|
||||
SecretName: "hello-hub-kubeconfig",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "cert-example-com-signer-name",
|
||||
VolumeSource: corev1.VolumeSource{
|
||||
Secret: &corev1.SecretVolumeSource{
|
||||
SecretName: "hello-example.com-signer-name-client-cert",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if !equality.Semantic.DeepEqual(volumes, expectedVolumes) {
|
||||
t.Errorf("expected volumes %v, but got: %v", expectedVolumes, volumes)
|
||||
}
|
||||
|
||||
volumeMounts := object.Spec.Template.Spec.Containers[0].VolumeMounts
|
||||
expectedVolumeMounts := []corev1.VolumeMount{
|
||||
{
|
||||
Name: "hub-kubeconfig",
|
||||
MountPath: "/managed/hub-kubeconfig",
|
||||
},
|
||||
{
|
||||
Name: "cert-example-com-signer-name",
|
||||
MountPath: "/managed/example.com-signer-name",
|
||||
},
|
||||
}
|
||||
if !equality.Semantic.DeepEqual(volumeMounts, expectedVolumeMounts) {
|
||||
t.Errorf("expected volumeMounts %v, but got: %v", expectedVolumeMounts, volumeMounts)
|
||||
}
|
||||
}
|
||||
116
pkg/addon/templateagent/testmanifests/addontemplate.yaml
Normal file
116
pkg/addon/templateagent/testmanifests/addontemplate.yaml
Normal file
@@ -0,0 +1,116 @@
|
||||
apiVersion: addon.open-cluster-management.io/v1alpha1
|
||||
kind: AddOnTemplate
|
||||
metadata:
|
||||
name: hello-template
|
||||
spec:
|
||||
addonName: hello-template
|
||||
agentSpec:
|
||||
workload:
|
||||
manifests:
|
||||
- kind: Deployment
|
||||
apiVersion: apps/v1
|
||||
metadata:
|
||||
name: hello-template-agent
|
||||
namespace: open-cluster-management-agent-addon
|
||||
annotations:
|
||||
"addon.open-cluster-management.io/deletion-orphan": ""
|
||||
labels:
|
||||
app: hello-template-agent
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: hello-template-agent
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: hello-template-agent
|
||||
spec:
|
||||
serviceAccountName: hello-template-agent-sa
|
||||
containers:
|
||||
- name: helloworld-agent
|
||||
image: quay.io/open-cluster-management/addon-examples:v1
|
||||
imagePullPolicy: IfNotPresent
|
||||
args:
|
||||
- "/helloworld_helm"
|
||||
- "agent"
|
||||
- "--cluster-name={{CLUSTER_NAME}}"
|
||||
- "--addon-namespace=open-cluster-management-agent-addon"
|
||||
- "--addon-name=hello-template"
|
||||
- "--hub-kubeconfig={{HUB_KUBECONFIG}}"
|
||||
env:
|
||||
- name: LOG_LEVEL
|
||||
value: "{{LOG_LEVEL}}" # addonDeploymentConfig variables
|
||||
- kind: ServiceAccount
|
||||
apiVersion: v1
|
||||
metadata:
|
||||
name: hello-template-agent-sa
|
||||
namespace: open-cluster-management-agent-addon
|
||||
annotations:
|
||||
"addon.open-cluster-management.io/deletion-orphan": ""
|
||||
- kind: ClusterRoleBinding
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
metadata:
|
||||
name: hello-template-agent
|
||||
roleRef:
|
||||
apiGroup: rbac.authorization.k8s.io
|
||||
kind: ClusterRole
|
||||
name: cluster-admin
|
||||
subjects:
|
||||
- kind: ServiceAccount
|
||||
name: hello-template-agent-sa
|
||||
namespace: open-cluster-management-agent-addon
|
||||
- kind: Job
|
||||
apiVersion: batch/v1
|
||||
metadata:
|
||||
name: hello-template-cleanup-configmap
|
||||
namespace: open-cluster-management-agent-addon
|
||||
annotations:
|
||||
"addon.open-cluster-management.io/addon-pre-delete": ""
|
||||
spec:
|
||||
manualSelector: true
|
||||
selector:
|
||||
matchLabels:
|
||||
job: hello-template-cleanup-configmap
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
job: hello-template-cleanup-configmap
|
||||
spec:
|
||||
serviceAccountName: hello-template-agent-sa
|
||||
restartPolicy: Never
|
||||
containers:
|
||||
- name: hello-template-agent
|
||||
image: quay.io/open-cluster-management/addon-examples
|
||||
imagePullPolicy: IfNotPresent
|
||||
args:
|
||||
- "/helloworld_helm"
|
||||
- "cleanup"
|
||||
- "--addon-namespace=open-cluster-management-agent-addon"
|
||||
registration:
|
||||
# kubeClient or custom signer, if kubeClient, user and group is in a certain format.
|
||||
# user is "system:open-cluster-management:cluster:{clusterName}:addon:{addonName}:agent:{agentName}"
|
||||
# group is ["system:open-cluster-management:cluster:{clusterName}:addon:{addonName}",
|
||||
# "system:open-cluster-management:addon:{addonName}", "system:authenticated"]
|
||||
- type: KubeClient
|
||||
kubeClient:
|
||||
hubPermissions:
|
||||
- type: CurrentCluster
|
||||
roleRef:
|
||||
apiGroup: rbac.authorization.k8s.io
|
||||
kind: ClusterRole
|
||||
name: cm-admin
|
||||
- customSigner:
|
||||
signerName: example.com/signer-name
|
||||
signingCA:
|
||||
name: ca-secret
|
||||
namespace: default
|
||||
subject:
|
||||
groups:
|
||||
- g1
|
||||
- g2
|
||||
organizationUnit:
|
||||
- o1
|
||||
- o2
|
||||
user: user1
|
||||
type: CustomSigner
|
||||
156
pkg/addon/templateagent/values.go
Normal file
156
pkg/addon/templateagent/values.go
Normal file
@@ -0,0 +1,156 @@
|
||||
package templateagent
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/addonfactory"
|
||||
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
clusterv1 "open-cluster-management.io/api/cluster/v1"
|
||||
)
|
||||
|
||||
// ToAddOnNodePlacementPrivateValues only transform the AddOnDeploymentConfig NodePlacement part into Values object
|
||||
// with a specific key, this value would be used by the addon template controller
|
||||
func ToAddOnNodePlacementPrivateValues(config addonapiv1alpha1.AddOnDeploymentConfig) (addonfactory.Values, error) {
|
||||
if config.Spec.NodePlacement == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return addonfactory.Values{
|
||||
NodePlacementPrivateValueKey: config.Spec.NodePlacement,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ToAddOnRegistriesPrivateValues only transform the AddOnDeploymentConfig Registries part into Values object
|
||||
// with a specific key, this value would be used by the addon template controller
|
||||
func ToAddOnRegistriesPrivateValues(config addonapiv1alpha1.AddOnDeploymentConfig) (addonfactory.Values, error) {
|
||||
if config.Spec.Registries == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return addonfactory.Values{
|
||||
RegistriesPrivateValueKey: config.Spec.Registries,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type keyValuePair struct {
|
||||
name string
|
||||
value string
|
||||
}
|
||||
|
||||
type orderedValues []keyValuePair
|
||||
|
||||
func (a *CRDTemplateAgentAddon) getValues(
|
||||
cluster *clusterv1.ManagedCluster,
|
||||
addon *addonapiv1alpha1.ManagedClusterAddOn,
|
||||
template *addonapiv1alpha1.AddOnTemplate,
|
||||
) (orderedValues, map[string]interface{}, map[string]interface{}, error) {
|
||||
|
||||
presetValues := make([]keyValuePair, 0)
|
||||
overrideValues := map[string]interface{}{}
|
||||
privateValues := map[string]interface{}{}
|
||||
|
||||
defaultSortedKeys, defaultValues, err := a.getDefaultValues(cluster, addon, template)
|
||||
if err != nil {
|
||||
return presetValues, overrideValues, privateValues, nil
|
||||
}
|
||||
overrideValues = addonfactory.MergeValues(overrideValues, defaultValues)
|
||||
|
||||
privateValuesKeys := map[string]struct{}{
|
||||
NodePlacementPrivateValueKey: {},
|
||||
RegistriesPrivateValueKey: {},
|
||||
}
|
||||
|
||||
for i := 0; i < len(a.getValuesFuncs); i++ {
|
||||
if a.getValuesFuncs[i] != nil {
|
||||
userValues, err := a.getValuesFuncs[i](cluster, addon)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
publicValues := map[string]interface{}{}
|
||||
for k, v := range userValues {
|
||||
if _, ok := privateValuesKeys[k]; ok {
|
||||
privateValues[k] = v
|
||||
continue
|
||||
}
|
||||
publicValues[k] = v
|
||||
}
|
||||
|
||||
overrideValues = addonfactory.MergeValues(overrideValues, publicValues)
|
||||
}
|
||||
}
|
||||
builtinSortedKeys, builtinValues, err := a.getBuiltinValues(cluster, addon)
|
||||
if err != nil {
|
||||
return presetValues, overrideValues, privateValues, nil
|
||||
}
|
||||
overrideValues = addonfactory.MergeValues(overrideValues, builtinValues)
|
||||
|
||||
for k, v := range overrideValues {
|
||||
_, ok := v.(string)
|
||||
if !ok {
|
||||
return nil, nil, nil, fmt.Errorf("only support string type for variables, invalid key %s", k)
|
||||
}
|
||||
}
|
||||
|
||||
keys := append(defaultSortedKeys, builtinSortedKeys...)
|
||||
|
||||
for _, key := range keys {
|
||||
presetValues = append(presetValues, keyValuePair{
|
||||
name: key,
|
||||
value: overrideValues[key].(string),
|
||||
})
|
||||
}
|
||||
return presetValues, overrideValues, privateValues, nil
|
||||
}
|
||||
|
||||
func (a *CRDTemplateAgentAddon) getBuiltinValues(
|
||||
cluster *clusterv1.ManagedCluster,
|
||||
addon *addonapiv1alpha1.ManagedClusterAddOn) ([]string, addonfactory.Values, error) {
|
||||
builtinValues := templateCRDBuiltinValues{}
|
||||
builtinValues.ClusterName = cluster.GetName()
|
||||
|
||||
installNamespace := addon.Spec.InstallNamespace
|
||||
if len(installNamespace) == 0 {
|
||||
installNamespace = addonfactory.AddonDefaultInstallNamespace
|
||||
}
|
||||
builtinValues.AddonInstallNamespace = installNamespace
|
||||
|
||||
value, err := addonfactory.JsonStructToValues(builtinValues)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return a.sortValueKeys(value), value, nil
|
||||
}
|
||||
|
||||
func (a *CRDTemplateAgentAddon) getDefaultValues(
|
||||
cluster *clusterv1.ManagedCluster,
|
||||
addon *addonapiv1alpha1.ManagedClusterAddOn,
|
||||
template *addonapiv1alpha1.AddOnTemplate) ([]string, addonfactory.Values, error) {
|
||||
defaultValues := templateCRDDefaultValues{}
|
||||
|
||||
// TODO: hubKubeConfigSecret depends on the signer configuration in registration, and the registration is an array.
|
||||
if template.Spec.Registration != nil {
|
||||
defaultValues.HubKubeConfigPath = hubKubeconfigPath()
|
||||
}
|
||||
|
||||
value, err := addonfactory.JsonStructToValues(defaultValues)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return a.sortValueKeys(value), value, nil
|
||||
}
|
||||
|
||||
func (a *CRDTemplateAgentAddon) sortValueKeys(value addonfactory.Values) []string {
|
||||
keys := make([]string, 0)
|
||||
for k := range value {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
|
||||
sort.Strings(keys)
|
||||
return keys
|
||||
}
|
||||
|
||||
func hubKubeconfigPath() string {
|
||||
return "/managed/hub-kubeconfig/kubeconfig"
|
||||
}
|
||||
20
pkg/cmd/hub/addon.go
Normal file
20
pkg/cmd/hub/addon.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package hub
|
||||
|
||||
import (
|
||||
"github.com/openshift/library-go/pkg/controller/controllercmd"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"open-cluster-management.io/ocm/pkg/addon"
|
||||
"open-cluster-management.io/ocm/pkg/version"
|
||||
)
|
||||
|
||||
// NewAddonManager generates a command to start addon manager
|
||||
func NewAddonManager() *cobra.Command {
|
||||
cmdConfig := controllercmd.
|
||||
NewControllerCommandConfig("manager", version.Get(), addon.RunManager)
|
||||
cmd := cmdConfig.NewCommand()
|
||||
cmd.Use = "manager"
|
||||
cmd.Short = "Start the Addon Manager"
|
||||
|
||||
return cmd
|
||||
}
|
||||
@@ -46,5 +46,10 @@ test-registration-operator-integration: ensure-kubebuilder-tools
|
||||
./registration-operator-integration.test -ginkgo.slow-spec-threshold=15s -ginkgo.v -ginkgo.fail-fast
|
||||
.PHONY: test-registration-operator-integration
|
||||
|
||||
test-integration: test-registration-operator-integration test-registration-integration test-placement-integration test-work-integration
|
||||
test-addon-integration: ensure-kubebuilder-tools
|
||||
go test -c ./test/integration/addon -o ./addon-integration.test
|
||||
./addon-integration.test -ginkgo.slow-spec-threshold=15s -ginkgo.v -ginkgo.fail-fast
|
||||
.PHONY: test-addon-integration
|
||||
|
||||
test-integration: test-registration-operator-integration test-registration-integration test-placement-integration test-work-integration test-addon-integration
|
||||
.PHONY: test-integration
|
||||
|
||||
488
test/integration/addon/addon_configs_test.go
Normal file
488
test/integration/addon/addon_configs_test.go
Normal file
@@ -0,0 +1,488 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
ginkgo "github.com/onsi/ginkgo/v2"
|
||||
"github.com/onsi/gomega"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/util/rand"
|
||||
|
||||
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
clusterv1 "open-cluster-management.io/api/cluster/v1"
|
||||
clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
|
||||
)
|
||||
|
||||
var _ = ginkgo.Describe("AddConfigs", func() {
|
||||
var managedClusterName string
|
||||
var configDefaultNamespace string
|
||||
var configDefaultName string
|
||||
var err error
|
||||
|
||||
ginkgo.BeforeEach(func() {
|
||||
suffix := rand.String(5)
|
||||
managedClusterName = fmt.Sprintf("managedcluster-%s", suffix)
|
||||
configDefaultNamespace = fmt.Sprintf("default-config-%s", suffix)
|
||||
configDefaultName = fmt.Sprintf("default-config-%s", suffix)
|
||||
|
||||
// prepare cluster
|
||||
managedCluster := &clusterv1.ManagedCluster{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: managedClusterName,
|
||||
},
|
||||
Spec: clusterv1.ManagedClusterSpec{
|
||||
HubAcceptsClient: true,
|
||||
},
|
||||
}
|
||||
_, err = hubClusterClient.ClusterV1().ManagedClusters().Create(context.Background(), managedCluster, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
clusterNS := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: managedClusterName}}
|
||||
_, err = hubKubeClient.CoreV1().Namespaces().Create(context.Background(), clusterNS, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
// prepare ClusterManagementAddon
|
||||
_, err = createClusterManagementAddOn(testAddOnConfigsImpl.name, configDefaultNamespace, configDefaultName)
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
// prepare default config
|
||||
configDefaultNS := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: configDefaultNamespace}}
|
||||
_, err = hubKubeClient.CoreV1().Namespaces().Create(context.Background(), configDefaultNS, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
addOnDefaultConfig := &addonapiv1alpha1.AddOnDeploymentConfig{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: configDefaultName,
|
||||
Namespace: configDefaultNamespace,
|
||||
},
|
||||
Spec: addOnDefaultConfigSpec,
|
||||
}
|
||||
_, err = hubAddonClient.AddonV1alpha1().AddOnDeploymentConfigs(configDefaultNamespace).Create(context.Background(), addOnDefaultConfig, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
})
|
||||
|
||||
ginkgo.AfterEach(func() {
|
||||
err = hubKubeClient.CoreV1().Namespaces().Delete(context.Background(), managedClusterName, metav1.DeleteOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
err = hubClusterClient.ClusterV1().ManagedClusters().Delete(context.Background(), managedClusterName, metav1.DeleteOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
err = hubKubeClient.CoreV1().Namespaces().Delete(context.Background(), configDefaultNamespace, metav1.DeleteOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
err = hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Delete(context.Background(), testAddOnConfigsImpl.name, metav1.DeleteOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
delete(testAddOnConfigsImpl.registrations, managedClusterName)
|
||||
})
|
||||
|
||||
ginkgo.It("Should use default config", func() {
|
||||
addon := &addonapiv1alpha1.ManagedClusterAddOn{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: testAddOnConfigsImpl.name,
|
||||
Namespace: managedClusterName,
|
||||
},
|
||||
Spec: addonapiv1alpha1.ManagedClusterAddOnSpec{
|
||||
InstallNamespace: "test",
|
||||
},
|
||||
}
|
||||
_, err = hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Create(context.Background(), addon, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
// check cma status
|
||||
assertClusterManagementAddOnDefaultConfigReferences(testAddOnConfigsImpl.name, addonapiv1alpha1.DefaultConfigReference{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
SpecHash: addOnDefaultConfigSpecHash,
|
||||
},
|
||||
})
|
||||
assertClusterManagementAddOnInstallProgression(testAddOnConfigsImpl.name)
|
||||
|
||||
// check mca status
|
||||
assertManagedClusterAddOnConfigReferences(testAddOnConfigsImpl.name, managedClusterName, addonapiv1alpha1.ConfigReference{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
LastObservedGeneration: 1,
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
SpecHash: addOnDefaultConfigSpecHash,
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.It("Should override default config by install strategy", func() {
|
||||
cma, err := hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Get(context.Background(), testAddOnConfigsImpl.name, metav1.GetOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
cma.Annotations = map[string]string{
|
||||
addonapiv1alpha1.AddonLifecycleAnnotationKey: addonapiv1alpha1.AddonLifecycleAddonManagerAnnotationValue,
|
||||
}
|
||||
cma.Spec.InstallStrategy = addonapiv1alpha1.InstallStrategy{
|
||||
Type: addonapiv1alpha1.AddonInstallStrategyPlacements,
|
||||
Placements: []addonapiv1alpha1.PlacementStrategy{
|
||||
{
|
||||
PlacementRef: addonapiv1alpha1.PlacementRef{Name: "test-placement", Namespace: configDefaultNamespace},
|
||||
Configs: []addonapiv1alpha1.AddOnConfig{
|
||||
{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: "another-config",
|
||||
},
|
||||
},
|
||||
},
|
||||
RolloutStrategy: addonapiv1alpha1.RolloutStrategy{
|
||||
Type: addonapiv1alpha1.AddonRolloutStrategyUpdateAll,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
updateClusterManagementAddOn(context.Background(), cma)
|
||||
|
||||
placement := &clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: "test-placement", Namespace: configDefaultNamespace}}
|
||||
_, err = hubClusterClient.ClusterV1beta1().Placements(configDefaultNamespace).Create(context.Background(), placement, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
decision := &clusterv1beta1.PlacementDecision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-placement",
|
||||
Namespace: configDefaultNamespace,
|
||||
Labels: map[string]string{clusterv1beta1.PlacementLabel: "test-placement"},
|
||||
},
|
||||
}
|
||||
decision, err = hubClusterClient.ClusterV1beta1().PlacementDecisions(configDefaultNamespace).Create(context.Background(), decision, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
decision.Status.Decisions = []clusterv1beta1.ClusterDecision{
|
||||
{ClusterName: managedClusterName},
|
||||
}
|
||||
_, err = hubClusterClient.ClusterV1beta1().PlacementDecisions(configDefaultNamespace).UpdateStatus(context.Background(), decision, metav1.UpdateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
// check cma status
|
||||
assertClusterManagementAddOnDefaultConfigReferences(testAddOnConfigsImpl.name, addonapiv1alpha1.DefaultConfigReference{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
SpecHash: addOnDefaultConfigSpecHash,
|
||||
},
|
||||
})
|
||||
assertClusterManagementAddOnInstallProgression(testAddOnConfigsImpl.name, addonapiv1alpha1.InstallProgression{
|
||||
PlacementRef: addonapiv1alpha1.PlacementRef{Name: "test-placement", Namespace: configDefaultNamespace},
|
||||
ConfigReferences: []addonapiv1alpha1.InstallConfigReference{
|
||||
{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: "another-config",
|
||||
},
|
||||
SpecHash: "",
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// check mca status
|
||||
assertManagedClusterAddOnConfigReferences(testAddOnConfigsImpl.name, managedClusterName, addonapiv1alpha1.ConfigReference{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: "another-config",
|
||||
},
|
||||
LastObservedGeneration: 0,
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: "another-config",
|
||||
},
|
||||
SpecHash: "",
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.It("Should override default config", func() {
|
||||
addOnConfig := &addonapiv1alpha1.AddOnDeploymentConfig{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "addon-config",
|
||||
Namespace: managedClusterName,
|
||||
},
|
||||
Spec: addOnTest1ConfigSpec,
|
||||
}
|
||||
_, err = hubAddonClient.AddonV1alpha1().AddOnDeploymentConfigs(managedClusterName).Create(context.Background(), addOnConfig, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
addon := &addonapiv1alpha1.ManagedClusterAddOn{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: testAddOnConfigsImpl.name,
|
||||
Namespace: managedClusterName,
|
||||
},
|
||||
Spec: addonapiv1alpha1.ManagedClusterAddOnSpec{
|
||||
InstallNamespace: "test",
|
||||
Configs: []addonapiv1alpha1.AddOnConfig{
|
||||
{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Name: addOnConfig.Name,
|
||||
Namespace: addOnConfig.Namespace,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
_, err = hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Create(context.Background(), addon, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
addon.Status = addonapiv1alpha1.ManagedClusterAddOnStatus{
|
||||
SupportedConfigs: []addonapiv1alpha1.ConfigGroupResource{
|
||||
{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
},
|
||||
}
|
||||
updateManagedClusterAddOnStatus(context.Background(), addon)
|
||||
|
||||
// check cma status
|
||||
assertClusterManagementAddOnDefaultConfigReferences(testAddOnConfigsImpl.name, addonapiv1alpha1.DefaultConfigReference{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
SpecHash: addOnDefaultConfigSpecHash,
|
||||
},
|
||||
})
|
||||
assertClusterManagementAddOnInstallProgression(testAddOnConfigsImpl.name)
|
||||
|
||||
// check mca status
|
||||
assertManagedClusterAddOnConfigReferences(testAddOnConfigsImpl.name, managedClusterName, addonapiv1alpha1.ConfigReference{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: addOnConfig.Namespace,
|
||||
Name: addOnConfig.Name,
|
||||
},
|
||||
LastObservedGeneration: 1,
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: addOnConfig.Namespace,
|
||||
Name: addOnConfig.Name,
|
||||
},
|
||||
SpecHash: addOnTest1ConfigSpecHash,
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.It("Should update config spec successfully", func() {
|
||||
addOnConfig := &addonapiv1alpha1.AddOnDeploymentConfig{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "addon-config",
|
||||
Namespace: managedClusterName,
|
||||
},
|
||||
Spec: addOnTest1ConfigSpec,
|
||||
}
|
||||
_, err = hubAddonClient.AddonV1alpha1().AddOnDeploymentConfigs(managedClusterName).Create(context.Background(), addOnConfig, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
addon := &addonapiv1alpha1.ManagedClusterAddOn{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: testAddOnConfigsImpl.name,
|
||||
Namespace: managedClusterName,
|
||||
},
|
||||
Spec: addonapiv1alpha1.ManagedClusterAddOnSpec{
|
||||
InstallNamespace: "test",
|
||||
Configs: []addonapiv1alpha1.AddOnConfig{
|
||||
{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Name: addOnConfig.Name,
|
||||
Namespace: addOnConfig.Namespace,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
addon, err = hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Create(context.Background(), addon, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
addon.Status = addonapiv1alpha1.ManagedClusterAddOnStatus{
|
||||
SupportedConfigs: []addonapiv1alpha1.ConfigGroupResource{
|
||||
{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
},
|
||||
}
|
||||
updateManagedClusterAddOnStatus(context.Background(), addon)
|
||||
|
||||
// check mca status
|
||||
assertManagedClusterAddOnConfigReferences(testAddOnConfigsImpl.name, managedClusterName, addonapiv1alpha1.ConfigReference{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: addOnConfig.Namespace,
|
||||
Name: addOnConfig.Name,
|
||||
},
|
||||
LastObservedGeneration: 1,
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: addOnConfig.Namespace,
|
||||
Name: addOnConfig.Name,
|
||||
},
|
||||
SpecHash: addOnTest1ConfigSpecHash,
|
||||
},
|
||||
})
|
||||
|
||||
addOnConfig, err = hubAddonClient.AddonV1alpha1().AddOnDeploymentConfigs(managedClusterName).Get(context.Background(), addOnConfig.Name, metav1.GetOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
addOnConfig.Spec = addOnTest2ConfigSpec
|
||||
_, err = hubAddonClient.AddonV1alpha1().AddOnDeploymentConfigs(managedClusterName).Update(context.Background(), addOnConfig, metav1.UpdateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
// check mca status
|
||||
assertManagedClusterAddOnConfigReferences(testAddOnConfigsImpl.name, managedClusterName, addonapiv1alpha1.ConfigReference{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: addOnConfig.Namespace,
|
||||
Name: addOnConfig.Name,
|
||||
},
|
||||
LastObservedGeneration: 2,
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: addOnConfig.Namespace,
|
||||
Name: addOnConfig.Name,
|
||||
},
|
||||
SpecHash: addOnTest2ConfigSpecHash,
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.It("Should not update unsupported config spec hash", func() {
|
||||
addOnConfig := &addonapiv1alpha1.AddOnDeploymentConfig{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "addon-config",
|
||||
Namespace: managedClusterName,
|
||||
},
|
||||
Spec: addOnTest1ConfigSpec,
|
||||
}
|
||||
_, err = hubAddonClient.AddonV1alpha1().AddOnDeploymentConfigs(managedClusterName).Create(context.Background(), addOnConfig, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
// empty supported config
|
||||
supportedConfig := testAddOnConfigsImpl.supportedConfigGVRs
|
||||
testAddOnConfigsImpl.supportedConfigGVRs = []schema.GroupVersionResource{}
|
||||
|
||||
// do not update mca status.SupportedConfigs
|
||||
addon := &addonapiv1alpha1.ManagedClusterAddOn{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: testAddOnConfigsImpl.name,
|
||||
Namespace: managedClusterName,
|
||||
},
|
||||
Spec: addonapiv1alpha1.ManagedClusterAddOnSpec{
|
||||
InstallNamespace: "test",
|
||||
Configs: []addonapiv1alpha1.AddOnConfig{
|
||||
{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Name: addOnConfig.Name,
|
||||
Namespace: addOnConfig.Namespace,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
_, err = hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Create(context.Background(), addon, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
// check cma status
|
||||
assertClusterManagementAddOnDefaultConfigReferences(testAddOnConfigsImpl.name, addonapiv1alpha1.DefaultConfigReference{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
SpecHash: addOnDefaultConfigSpecHash,
|
||||
},
|
||||
})
|
||||
assertClusterManagementAddOnInstallProgression(testAddOnConfigsImpl.name)
|
||||
|
||||
// check mca status
|
||||
assertManagedClusterAddOnConfigReferences(testAddOnConfigsImpl.name, managedClusterName, addonapiv1alpha1.ConfigReference{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: addOnConfig.Namespace,
|
||||
Name: addOnConfig.Name,
|
||||
},
|
||||
LastObservedGeneration: 1,
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: addOnConfig.Namespace,
|
||||
Name: addOnConfig.Name,
|
||||
},
|
||||
SpecHash: "",
|
||||
},
|
||||
})
|
||||
|
||||
testAddOnConfigsImpl.supportedConfigGVRs = supportedConfig
|
||||
})
|
||||
})
|
||||
174
test/integration/addon/addon_manager_install_test.go
Normal file
174
test/integration/addon/addon_manager_install_test.go
Normal file
@@ -0,0 +1,174 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
ginkgo "github.com/onsi/ginkgo/v2"
|
||||
"github.com/onsi/gomega"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/rand"
|
||||
|
||||
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
clusterv1 "open-cluster-management.io/api/cluster/v1"
|
||||
clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
|
||||
)
|
||||
|
||||
var _ = ginkgo.Describe("Agent deploy", func() {
|
||||
suffix := rand.String(5)
|
||||
var cma *addonapiv1alpha1.ClusterManagementAddOn
|
||||
var placementNamespace string
|
||||
var clusterNames []string
|
||||
|
||||
ginkgo.BeforeEach(func() {
|
||||
// Create clustermanagement addon
|
||||
cma = &addonapiv1alpha1.ClusterManagementAddOn{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: fmt.Sprintf("test-%s", suffix),
|
||||
Annotations: map[string]string{
|
||||
addonapiv1alpha1.AddonLifecycleAnnotationKey: addonapiv1alpha1.AddonLifecycleAddonManagerAnnotationValue,
|
||||
},
|
||||
},
|
||||
Spec: addonapiv1alpha1.ClusterManagementAddOnSpec{
|
||||
InstallStrategy: addonapiv1alpha1.InstallStrategy{
|
||||
Type: addonapiv1alpha1.AddonInstallStrategyManual,
|
||||
},
|
||||
},
|
||||
}
|
||||
_, err := hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Create(context.Background(), cma, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
placementNamespace = fmt.Sprintf("ns-%s", suffix)
|
||||
ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: placementNamespace}}
|
||||
_, err = hubKubeClient.CoreV1().Namespaces().Create(context.Background(), ns, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
for i := 0; i < 4; i++ {
|
||||
managedClusterName := fmt.Sprintf("managedcluster-%s-%d", suffix, i)
|
||||
clusterNames = append(clusterNames, managedClusterName)
|
||||
managedCluster := &clusterv1.ManagedCluster{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: managedClusterName,
|
||||
},
|
||||
Spec: clusterv1.ManagedClusterSpec{
|
||||
HubAcceptsClient: true,
|
||||
},
|
||||
}
|
||||
_, err = hubClusterClient.ClusterV1().ManagedClusters().Create(context.Background(), managedCluster, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: managedClusterName}}
|
||||
_, err = hubKubeClient.CoreV1().Namespaces().Create(context.Background(), ns, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
}
|
||||
})
|
||||
|
||||
ginkgo.AfterEach(func() {
|
||||
err := hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Delete(context.Background(), cma.Name, metav1.DeleteOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
err = hubKubeClient.CoreV1().Namespaces().Delete(context.Background(), placementNamespace, metav1.DeleteOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
for _, managedClusterName := range clusterNames {
|
||||
err = hubKubeClient.CoreV1().Namespaces().Delete(context.Background(), managedClusterName, metav1.DeleteOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
err = hubClusterClient.ClusterV1().ManagedClusters().Delete(context.Background(), managedClusterName, metav1.DeleteOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
}
|
||||
})
|
||||
|
||||
ginkgo.Context("Addon install strategy", func() {
|
||||
ginkgo.It("Should create/delete mca correctly by placement", func() {
|
||||
placement := &clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: "test-placement", Namespace: placementNamespace}}
|
||||
_, err := hubClusterClient.ClusterV1beta1().Placements(placementNamespace).Create(context.Background(), placement, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
decision := &clusterv1beta1.PlacementDecision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-placement",
|
||||
Namespace: placementNamespace,
|
||||
Labels: map[string]string{clusterv1beta1.PlacementLabel: "test-placement"},
|
||||
},
|
||||
}
|
||||
decision, err = hubClusterClient.ClusterV1beta1().PlacementDecisions(placementNamespace).Create(context.Background(), decision, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
decision.Status.Decisions = []clusterv1beta1.ClusterDecision{
|
||||
{ClusterName: clusterNames[0]},
|
||||
{ClusterName: clusterNames[1]},
|
||||
}
|
||||
_, err = hubClusterClient.ClusterV1beta1().PlacementDecisions(placementNamespace).UpdateStatus(context.Background(), decision, metav1.UpdateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
clusterManagementAddon, err := hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Get(context.Background(), cma.Name, metav1.GetOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
clusterManagementAddon.Spec.InstallStrategy = addonapiv1alpha1.InstallStrategy{
|
||||
Type: addonapiv1alpha1.AddonInstallStrategyPlacements,
|
||||
Placements: []addonapiv1alpha1.PlacementStrategy{
|
||||
{
|
||||
PlacementRef: addonapiv1alpha1.PlacementRef{Name: "test-placement", Namespace: placementNamespace},
|
||||
RolloutStrategy: addonapiv1alpha1.RolloutStrategy{
|
||||
Type: addonapiv1alpha1.AddonRolloutStrategyUpdateAll,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
_, err = hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Update(context.Background(), clusterManagementAddon, metav1.UpdateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
gomega.Eventually(func() error {
|
||||
_, err := hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(clusterNames[0]).Get(context.Background(), cma.Name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(clusterNames[1]).Get(context.Background(), cma.Name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())
|
||||
|
||||
// Update the decision
|
||||
decision, err = hubClusterClient.ClusterV1beta1().PlacementDecisions(placementNamespace).Get(context.Background(), "test-placement", metav1.GetOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
decision.Status.Decisions = []clusterv1beta1.ClusterDecision{
|
||||
{ClusterName: clusterNames[1]},
|
||||
{ClusterName: clusterNames[2]},
|
||||
}
|
||||
_, err = hubClusterClient.ClusterV1beta1().PlacementDecisions(placementNamespace).UpdateStatus(context.Background(), decision, metav1.UpdateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
gomega.Eventually(func() error {
|
||||
_, err := hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(clusterNames[1]).Get(context.Background(), cma.Name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(clusterNames[2]).Get(context.Background(), cma.Name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(clusterNames[0]).Get(context.Background(), cma.Name, metav1.GetOptions{})
|
||||
if !errors.IsNotFound(err) {
|
||||
return fmt.Errorf("addon in cluster %s should be removed", clusterNames[0])
|
||||
}
|
||||
return nil
|
||||
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())
|
||||
|
||||
// delete an addon and ensure it is recreated.
|
||||
err = hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(clusterNames[1]).Delete(context.Background(), cma.Name, metav1.DeleteOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
gomega.Eventually(func() error {
|
||||
_, err := hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(clusterNames[1]).Get(context.Background(), cma.Name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
693
test/integration/addon/addon_manager_upgrade_test.go
Normal file
693
test/integration/addon/addon_manager_upgrade_test.go
Normal file
@@ -0,0 +1,693 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
ginkgo "github.com/onsi/ginkgo/v2"
|
||||
"github.com/onsi/gomega"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
"k8s.io/apimachinery/pkg/util/rand"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/addonmanager/constants"
|
||||
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
clusterv1 "open-cluster-management.io/api/cluster/v1"
|
||||
clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
|
||||
workapiv1 "open-cluster-management.io/api/work/v1"
|
||||
)
|
||||
|
||||
const (
|
||||
upgradeDeploymentJson = `{
|
||||
"apiVersion": "apps/v1",
|
||||
"kind": "Deployment",
|
||||
"metadata": {
|
||||
"name": "nginx-deployment",
|
||||
"namespace": "default"
|
||||
},
|
||||
"spec": {
|
||||
"replicas": 1,
|
||||
"selector": {
|
||||
"matchLabels": {
|
||||
"app": "nginx"
|
||||
}
|
||||
},
|
||||
"template": {
|
||||
"metadata": {
|
||||
"creationTimestamp": null,
|
||||
"labels": {
|
||||
"app": "nginx"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"containers": [
|
||||
{
|
||||
"image": "nginx:1.14.2",
|
||||
"name": "nginx",
|
||||
"ports": [
|
||||
{
|
||||
"containerPort": 80,
|
||||
"protocol": "TCP"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}`
|
||||
)
|
||||
|
||||
var _ = ginkgo.Describe("Addon upgrade", func() {
|
||||
var configDefaultNamespace string
|
||||
var configDefaultName string
|
||||
var configUpdateName string
|
||||
var placementName string
|
||||
var placementNamespace string
|
||||
var manifestWorkName string
|
||||
var clusterNames []string
|
||||
var suffix string
|
||||
var err error
|
||||
var cma *addonapiv1alpha1.ClusterManagementAddOn
|
||||
|
||||
ginkgo.BeforeEach(func() {
|
||||
suffix = rand.String(5)
|
||||
configDefaultNamespace = fmt.Sprintf("default-config-%s", suffix)
|
||||
configDefaultName = fmt.Sprintf("default-config-%s", suffix)
|
||||
configUpdateName = fmt.Sprintf("update-config-%s", suffix)
|
||||
placementName = fmt.Sprintf("ns-%s", suffix)
|
||||
placementNamespace = fmt.Sprintf("ns-%s", suffix)
|
||||
manifestWorkName = fmt.Sprintf("%s-0", constants.DeployWorkNamePrefix(testAddOnConfigsImpl.name))
|
||||
|
||||
// prepare cma
|
||||
cma = &addonapiv1alpha1.ClusterManagementAddOn{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: testAddOnConfigsImpl.name,
|
||||
Annotations: map[string]string{
|
||||
addonapiv1alpha1.AddonLifecycleAnnotationKey: addonapiv1alpha1.AddonLifecycleAddonManagerAnnotationValue,
|
||||
},
|
||||
},
|
||||
Spec: addonapiv1alpha1.ClusterManagementAddOnSpec{
|
||||
InstallStrategy: addonapiv1alpha1.InstallStrategy{
|
||||
Type: addonapiv1alpha1.AddonInstallStrategyPlacements,
|
||||
Placements: []addonapiv1alpha1.PlacementStrategy{
|
||||
{
|
||||
PlacementRef: addonapiv1alpha1.PlacementRef{Name: placementName, Namespace: placementNamespace},
|
||||
RolloutStrategy: addonapiv1alpha1.RolloutStrategy{
|
||||
Type: addonapiv1alpha1.AddonRolloutStrategyUpdateAll,
|
||||
},
|
||||
Configs: []addonapiv1alpha1.AddOnConfig{
|
||||
{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
_, err := hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Create(context.Background(), cma, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
// prepare cluster
|
||||
for i := 0; i < 4; i++ {
|
||||
managedClusterName := fmt.Sprintf("managedcluster-%s-%d", suffix, i)
|
||||
clusterNames = append(clusterNames, managedClusterName)
|
||||
managedCluster := &clusterv1.ManagedCluster{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: managedClusterName,
|
||||
},
|
||||
Spec: clusterv1.ManagedClusterSpec{
|
||||
HubAcceptsClient: true,
|
||||
},
|
||||
}
|
||||
_, err = hubClusterClient.ClusterV1().ManagedClusters().Create(context.Background(), managedCluster, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: managedClusterName}}
|
||||
_, err = hubKubeClient.CoreV1().Namespaces().Create(context.Background(), ns, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
}
|
||||
|
||||
// prepare manifestwork obj
|
||||
for i := 0; i < 4; i++ {
|
||||
obj := &unstructured.Unstructured{}
|
||||
err := obj.UnmarshalJSON([]byte(upgradeDeploymentJson))
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
testAddOnConfigsImpl.manifests[clusterNames[i]] = []runtime.Object{obj}
|
||||
}
|
||||
|
||||
// prepare placement
|
||||
ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: placementNamespace}}
|
||||
_, err = hubKubeClient.CoreV1().Namespaces().Create(context.Background(), ns, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
placement := &clusterv1beta1.Placement{ObjectMeta: metav1.ObjectMeta{Name: placementName, Namespace: placementNamespace}}
|
||||
_, err = hubClusterClient.ClusterV1beta1().Placements(placementNamespace).Create(context.Background(), placement, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
decision := &clusterv1beta1.PlacementDecision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: placementName,
|
||||
Namespace: placementNamespace,
|
||||
Labels: map[string]string{clusterv1beta1.PlacementLabel: placementName},
|
||||
},
|
||||
}
|
||||
decision, err = hubClusterClient.ClusterV1beta1().PlacementDecisions(placementNamespace).Create(context.Background(), decision, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
decision.Status.Decisions = []clusterv1beta1.ClusterDecision{
|
||||
{ClusterName: clusterNames[0]},
|
||||
{ClusterName: clusterNames[1]},
|
||||
{ClusterName: clusterNames[2]},
|
||||
{ClusterName: clusterNames[3]},
|
||||
}
|
||||
_, err = hubClusterClient.ClusterV1beta1().PlacementDecisions(placementNamespace).UpdateStatus(context.Background(), decision, metav1.UpdateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
// prepare default config
|
||||
configDefaultNS := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: configDefaultNamespace}}
|
||||
_, err = hubKubeClient.CoreV1().Namespaces().Create(context.Background(), configDefaultNS, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
addOnDefaultConfig := &addonapiv1alpha1.AddOnDeploymentConfig{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: configDefaultName,
|
||||
Namespace: configDefaultNamespace,
|
||||
},
|
||||
Spec: addOnDefaultConfigSpec,
|
||||
}
|
||||
_, err = hubAddonClient.AddonV1alpha1().AddOnDeploymentConfigs(configDefaultNamespace).Create(context.Background(), addOnDefaultConfig, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
// prepare update config
|
||||
addOnUpdateConfig := &addonapiv1alpha1.AddOnDeploymentConfig{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: configUpdateName,
|
||||
Namespace: configDefaultNamespace,
|
||||
},
|
||||
Spec: addOnTest2ConfigSpec,
|
||||
}
|
||||
_, err = hubAddonClient.AddonV1alpha1().AddOnDeploymentConfigs(configDefaultNamespace).Create(context.Background(), addOnUpdateConfig, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
})
|
||||
|
||||
ginkgo.AfterEach(func() {
|
||||
err = hubKubeClient.CoreV1().Namespaces().Delete(context.Background(), configDefaultNamespace, metav1.DeleteOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
err = hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Delete(context.Background(), testAddOnConfigsImpl.name, metav1.DeleteOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
for _, managedClusterName := range clusterNames {
|
||||
err = hubKubeClient.CoreV1().Namespaces().Delete(context.Background(), managedClusterName, metav1.DeleteOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
err = hubClusterClient.ClusterV1().ManagedClusters().Delete(context.Background(), managedClusterName, metav1.DeleteOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
delete(testAddOnConfigsImpl.registrations, managedClusterName)
|
||||
}
|
||||
})
|
||||
|
||||
ginkgo.Context("Addon rollout strategy", func() {
|
||||
ginkgo.It("Should update when config changes", func() {
|
||||
ginkgo.By("fresh install")
|
||||
ginkgo.By("check work")
|
||||
gomega.Eventually(func() error {
|
||||
for i := 0; i < 4; i++ {
|
||||
work, err := hubWorkClient.WorkV1().ManifestWorks(clusterNames[i]).Get(context.Background(), manifestWorkName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(work.Annotations) == 0 {
|
||||
return fmt.Errorf("Unexpected number of work annotations %v", work.Annotations)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())
|
||||
|
||||
ginkgo.By("update work status to trigger addon status")
|
||||
for i := 0; i < 4; i++ {
|
||||
work, err := hubWorkClient.WorkV1().ManifestWorks(clusterNames[i]).Get(context.Background(), manifestWorkName, metav1.GetOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
meta.SetStatusCondition(&work.Status.Conditions, metav1.Condition{Type: workapiv1.WorkApplied, Status: metav1.ConditionTrue, Reason: "WorkApplied", ObservedGeneration: work.Generation})
|
||||
meta.SetStatusCondition(&work.Status.Conditions, metav1.Condition{Type: workapiv1.WorkAvailable, Status: metav1.ConditionTrue, Reason: "WorkAvailable", ObservedGeneration: work.Generation})
|
||||
_, err = hubWorkClient.WorkV1().ManifestWorks(clusterNames[i]).UpdateStatus(context.Background(), work, metav1.UpdateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
}
|
||||
|
||||
ginkgo.By("check mca status")
|
||||
for i := 0; i < 4; i++ {
|
||||
assertManagedClusterAddOnConfigReferences(testAddOnConfigsImpl.name, clusterNames[i], addonapiv1alpha1.ConfigReference{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
LastObservedGeneration: 1,
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
SpecHash: addOnDefaultConfigSpecHash,
|
||||
},
|
||||
LastAppliedConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
SpecHash: addOnDefaultConfigSpecHash,
|
||||
},
|
||||
})
|
||||
assertManagedClusterAddOnConditions(testAddOnConfigsImpl.name, clusterNames[i], metav1.Condition{
|
||||
Type: addonapiv1alpha1.ManagedClusterAddOnConditionProgressing,
|
||||
Status: metav1.ConditionFalse,
|
||||
Reason: addonapiv1alpha1.ProgressingReasonInstallSucceed,
|
||||
Message: "install completed with no errors.",
|
||||
})
|
||||
}
|
||||
|
||||
ginkgo.By("check cma status")
|
||||
assertClusterManagementAddOnInstallProgression(testAddOnConfigsImpl.name, addonapiv1alpha1.InstallProgression{
|
||||
PlacementRef: addonapiv1alpha1.PlacementRef{Name: placementNamespace, Namespace: placementNamespace},
|
||||
ConfigReferences: []addonapiv1alpha1.InstallConfigReference{
|
||||
{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
SpecHash: addOnDefaultConfigSpecHash,
|
||||
},
|
||||
LastAppliedConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
SpecHash: addOnDefaultConfigSpecHash,
|
||||
},
|
||||
LastKnownGoodConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
SpecHash: addOnDefaultConfigSpecHash,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
assertClusterManagementAddOnConditions(testAddOnConfigsImpl.name, metav1.Condition{
|
||||
Type: addonapiv1alpha1.ManagedClusterAddOnConditionProgressing,
|
||||
Status: metav1.ConditionFalse,
|
||||
Reason: addonapiv1alpha1.ProgressingReasonInstallSucceed,
|
||||
Message: "4/4 install completed with no errors.",
|
||||
})
|
||||
|
||||
ginkgo.By("update all")
|
||||
ginkgo.By("upgrade configs to test1")
|
||||
addOnConfig, err := hubAddonClient.AddonV1alpha1().AddOnDeploymentConfigs(configDefaultNamespace).Get(context.Background(), configDefaultName, metav1.GetOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
addOnConfig.Spec = addOnTest1ConfigSpec
|
||||
_, err = hubAddonClient.AddonV1alpha1().AddOnDeploymentConfigs(configDefaultNamespace).Update(context.Background(), addOnConfig, metav1.UpdateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
ginkgo.By("check mca status")
|
||||
for i := 0; i < 4; i++ {
|
||||
assertManagedClusterAddOnConfigReferences(testAddOnConfigsImpl.name, clusterNames[i], addonapiv1alpha1.ConfigReference{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
LastObservedGeneration: 2,
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
SpecHash: addOnTest1ConfigSpecHash,
|
||||
},
|
||||
LastAppliedConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
SpecHash: addOnTest1ConfigSpecHash,
|
||||
},
|
||||
})
|
||||
assertManagedClusterAddOnConditions(testAddOnConfigsImpl.name, clusterNames[i], metav1.Condition{
|
||||
Type: addonapiv1alpha1.ManagedClusterAddOnConditionProgressing,
|
||||
Status: metav1.ConditionFalse,
|
||||
Reason: addonapiv1alpha1.ProgressingReasonUpgradeSucceed,
|
||||
Message: "upgrade completed with no errors.",
|
||||
})
|
||||
}
|
||||
|
||||
ginkgo.By("check cma status")
|
||||
assertClusterManagementAddOnInstallProgression(testAddOnConfigsImpl.name, addonapiv1alpha1.InstallProgression{
|
||||
PlacementRef: addonapiv1alpha1.PlacementRef{Name: placementNamespace, Namespace: placementNamespace},
|
||||
ConfigReferences: []addonapiv1alpha1.InstallConfigReference{
|
||||
{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
SpecHash: addOnTest1ConfigSpecHash,
|
||||
},
|
||||
LastAppliedConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
SpecHash: addOnTest1ConfigSpecHash,
|
||||
},
|
||||
LastKnownGoodConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
SpecHash: addOnTest1ConfigSpecHash,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
assertClusterManagementAddOnConditions(testAddOnConfigsImpl.name, metav1.Condition{
|
||||
Type: addonapiv1alpha1.ManagedClusterAddOnConditionProgressing,
|
||||
Status: metav1.ConditionFalse,
|
||||
Reason: addonapiv1alpha1.ProgressingReasonUpgradeSucceed,
|
||||
Message: "4/4 upgrade completed with no errors.",
|
||||
})
|
||||
|
||||
ginkgo.By("update work status to avoid addon status update")
|
||||
gomega.Eventually(func() error {
|
||||
for i := 0; i < 4; i++ {
|
||||
work, err := hubWorkClient.WorkV1().ManifestWorks(clusterNames[i]).Get(context.Background(), manifestWorkName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
meta.SetStatusCondition(&work.Status.Conditions, metav1.Condition{Type: workapiv1.WorkApplied, Status: metav1.ConditionFalse, Reason: "WorkApplied", ObservedGeneration: work.Generation})
|
||||
meta.SetStatusCondition(&work.Status.Conditions, metav1.Condition{Type: workapiv1.WorkAvailable, Status: metav1.ConditionFalse, Reason: "WorkAvailable", ObservedGeneration: work.Generation})
|
||||
_, err = hubWorkClient.WorkV1().ManifestWorks(clusterNames[i]).UpdateStatus(context.Background(), work, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())
|
||||
|
||||
ginkgo.By("rolling upgrade")
|
||||
ginkgo.By("update cma to rolling update")
|
||||
cma, err = hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Get(context.Background(), cma.Name, metav1.GetOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
cma.Spec.InstallStrategy.Placements[0].RolloutStrategy.Type = addonapiv1alpha1.AddonRolloutStrategyRollingUpdate
|
||||
cma.Spec.InstallStrategy.Placements[0].RolloutStrategy.RollingUpdate = &addonapiv1alpha1.RollingUpdate{MaxConcurrency: intstr.FromString("50%")}
|
||||
cma.Spec.InstallStrategy.Placements[0].Configs[0].ConfigReferent = addonapiv1alpha1.ConfigReferent{Namespace: configDefaultNamespace, Name: configUpdateName}
|
||||
_, err = hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Update(context.Background(), cma, metav1.UpdateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
ginkgo.By("check mca status")
|
||||
for i := 0; i < 2; i++ {
|
||||
assertManagedClusterAddOnConfigReferences(testAddOnConfigsImpl.name, clusterNames[i], addonapiv1alpha1.ConfigReference{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configUpdateName,
|
||||
},
|
||||
LastObservedGeneration: 1,
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configUpdateName,
|
||||
},
|
||||
SpecHash: addOnTest2ConfigSpecHash,
|
||||
},
|
||||
LastAppliedConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
SpecHash: addOnTest1ConfigSpecHash,
|
||||
},
|
||||
})
|
||||
assertManagedClusterAddOnConditions(testAddOnConfigsImpl.name, clusterNames[i], metav1.Condition{
|
||||
Type: addonapiv1alpha1.ManagedClusterAddOnConditionProgressing,
|
||||
Status: metav1.ConditionTrue,
|
||||
Reason: addonapiv1alpha1.ProgressingReasonUpgrading,
|
||||
Message: "upgrading... work is not ready",
|
||||
})
|
||||
}
|
||||
for i := 2; i < 4; i++ {
|
||||
assertManagedClusterAddOnConfigReferences(testAddOnConfigsImpl.name, clusterNames[i], addonapiv1alpha1.ConfigReference{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
LastObservedGeneration: 2,
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
SpecHash: addOnTest1ConfigSpecHash,
|
||||
},
|
||||
LastAppliedConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
SpecHash: addOnTest1ConfigSpecHash,
|
||||
},
|
||||
})
|
||||
assertManagedClusterAddOnConditions(testAddOnConfigsImpl.name, clusterNames[i], metav1.Condition{
|
||||
Type: addonapiv1alpha1.ManagedClusterAddOnConditionProgressing,
|
||||
Status: metav1.ConditionFalse,
|
||||
Reason: addonapiv1alpha1.ProgressingReasonUpgradeSucceed,
|
||||
Message: "upgrade completed with no errors.",
|
||||
})
|
||||
}
|
||||
|
||||
ginkgo.By("check cma status")
|
||||
assertClusterManagementAddOnInstallProgression(testAddOnConfigsImpl.name, addonapiv1alpha1.InstallProgression{
|
||||
PlacementRef: addonapiv1alpha1.PlacementRef{Name: placementNamespace, Namespace: placementNamespace},
|
||||
ConfigReferences: []addonapiv1alpha1.InstallConfigReference{
|
||||
{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configUpdateName,
|
||||
},
|
||||
SpecHash: addOnTest2ConfigSpecHash,
|
||||
},
|
||||
LastAppliedConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
SpecHash: addOnTest1ConfigSpecHash,
|
||||
},
|
||||
LastKnownGoodConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
SpecHash: addOnTest1ConfigSpecHash,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
assertClusterManagementAddOnConditions(testAddOnConfigsImpl.name, metav1.Condition{
|
||||
Type: addonapiv1alpha1.ManagedClusterAddOnConditionProgressing,
|
||||
Status: metav1.ConditionTrue,
|
||||
Reason: addonapiv1alpha1.ProgressingReasonUpgrading,
|
||||
Message: "2/4 upgrading...",
|
||||
})
|
||||
|
||||
ginkgo.By("update 2 work status to trigger addon status")
|
||||
gomega.Eventually(func() error {
|
||||
for i := 0; i < 2; i++ {
|
||||
work, err := hubWorkClient.WorkV1().ManifestWorks(clusterNames[i]).Get(context.Background(), manifestWorkName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
meta.SetStatusCondition(&work.Status.Conditions, metav1.Condition{Type: workapiv1.WorkApplied, Status: metav1.ConditionTrue, Reason: "WorkApplied", ObservedGeneration: work.Generation})
|
||||
meta.SetStatusCondition(&work.Status.Conditions, metav1.Condition{Type: workapiv1.WorkAvailable, Status: metav1.ConditionTrue, Reason: "WorkAvailable", ObservedGeneration: work.Generation})
|
||||
_, err = hubWorkClient.WorkV1().ManifestWorks(clusterNames[i]).UpdateStatus(context.Background(), work, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())
|
||||
|
||||
ginkgo.By("check mca status")
|
||||
for i := 0; i < 2; i++ {
|
||||
assertManagedClusterAddOnConfigReferences(testAddOnConfigsImpl.name, clusterNames[i], addonapiv1alpha1.ConfigReference{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configUpdateName,
|
||||
},
|
||||
LastObservedGeneration: 1,
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configUpdateName,
|
||||
},
|
||||
SpecHash: addOnTest2ConfigSpecHash,
|
||||
},
|
||||
LastAppliedConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configUpdateName,
|
||||
},
|
||||
SpecHash: addOnTest2ConfigSpecHash,
|
||||
},
|
||||
})
|
||||
assertManagedClusterAddOnConditions(testAddOnConfigsImpl.name, clusterNames[i], metav1.Condition{
|
||||
Type: addonapiv1alpha1.ManagedClusterAddOnConditionProgressing,
|
||||
Status: metav1.ConditionFalse,
|
||||
Reason: addonapiv1alpha1.ProgressingReasonUpgradeSucceed,
|
||||
Message: "upgrade completed with no errors.",
|
||||
})
|
||||
}
|
||||
|
||||
ginkgo.By("check cma status")
|
||||
assertClusterManagementAddOnInstallProgression(testAddOnConfigsImpl.name, addonapiv1alpha1.InstallProgression{
|
||||
PlacementRef: addonapiv1alpha1.PlacementRef{Name: placementNamespace, Namespace: placementNamespace},
|
||||
ConfigReferences: []addonapiv1alpha1.InstallConfigReference{
|
||||
{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configUpdateName,
|
||||
},
|
||||
SpecHash: addOnTest2ConfigSpecHash,
|
||||
},
|
||||
LastAppliedConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
SpecHash: addOnTest1ConfigSpecHash,
|
||||
},
|
||||
LastKnownGoodConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configDefaultName,
|
||||
},
|
||||
SpecHash: addOnTest1ConfigSpecHash,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
assertClusterManagementAddOnConditions(testAddOnConfigsImpl.name, metav1.Condition{
|
||||
Type: addonapiv1alpha1.ManagedClusterAddOnConditionProgressing,
|
||||
Status: metav1.ConditionTrue,
|
||||
Reason: addonapiv1alpha1.ProgressingReasonUpgrading,
|
||||
Message: "4/4 upgrading...",
|
||||
})
|
||||
|
||||
ginkgo.By("update another 2 work status to trigger addon status")
|
||||
gomega.Eventually(func() error {
|
||||
for i := 2; i < 4; i++ {
|
||||
work, err := hubWorkClient.WorkV1().ManifestWorks(clusterNames[i]).Get(context.Background(), manifestWorkName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
meta.SetStatusCondition(&work.Status.Conditions, metav1.Condition{Type: workapiv1.WorkApplied, Status: metav1.ConditionTrue, Reason: "WorkApplied", ObservedGeneration: work.Generation})
|
||||
meta.SetStatusCondition(&work.Status.Conditions, metav1.Condition{Type: workapiv1.WorkAvailable, Status: metav1.ConditionTrue, Reason: "WorkAvailable", ObservedGeneration: work.Generation})
|
||||
_, err = hubWorkClient.WorkV1().ManifestWorks(clusterNames[i]).UpdateStatus(context.Background(), work, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())
|
||||
|
||||
ginkgo.By("check cma status")
|
||||
assertClusterManagementAddOnInstallProgression(testAddOnConfigsImpl.name, addonapiv1alpha1.InstallProgression{
|
||||
PlacementRef: addonapiv1alpha1.PlacementRef{Name: placementNamespace, Namespace: placementNamespace},
|
||||
ConfigReferences: []addonapiv1alpha1.InstallConfigReference{
|
||||
{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configUpdateName,
|
||||
},
|
||||
SpecHash: addOnTest2ConfigSpecHash,
|
||||
},
|
||||
LastAppliedConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configUpdateName,
|
||||
},
|
||||
SpecHash: addOnTest2ConfigSpecHash,
|
||||
},
|
||||
LastKnownGoodConfig: &addonapiv1alpha1.ConfigSpecHash{
|
||||
ConfigReferent: addonapiv1alpha1.ConfigReferent{
|
||||
Namespace: configDefaultNamespace,
|
||||
Name: configUpdateName,
|
||||
},
|
||||
SpecHash: addOnTest2ConfigSpecHash,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
assertClusterManagementAddOnConditions(testAddOnConfigsImpl.name, metav1.Condition{
|
||||
Type: addonapiv1alpha1.ManagedClusterAddOnConditionProgressing,
|
||||
Status: metav1.ConditionFalse,
|
||||
Reason: addonapiv1alpha1.ProgressingReasonUpgradeSucceed,
|
||||
Message: "4/4 upgrade completed with no errors.",
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
223
test/integration/addon/agent_deploy_test.go
Normal file
223
test/integration/addon/agent_deploy_test.go
Normal file
@@ -0,0 +1,223 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
ginkgo "github.com/onsi/ginkgo/v2"
|
||||
"github.com/onsi/gomega"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
apiequality "k8s.io/apimachinery/pkg/api/equality"
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/util/rand"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/addonmanager/constants"
|
||||
"open-cluster-management.io/addon-framework/pkg/agent"
|
||||
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
clusterv1 "open-cluster-management.io/api/cluster/v1"
|
||||
workapiv1 "open-cluster-management.io/api/work/v1"
|
||||
)
|
||||
|
||||
const (
|
||||
deploymentJson = `{
|
||||
"apiVersion": "apps/v1",
|
||||
"kind": "Deployment",
|
||||
"metadata": {
|
||||
"name": "nginx-deployment",
|
||||
"namespace": "default"
|
||||
},
|
||||
"spec": {
|
||||
"replicas": 1,
|
||||
"selector": {
|
||||
"matchLabels": {
|
||||
"app": "nginx"
|
||||
}
|
||||
},
|
||||
"template": {
|
||||
"metadata": {
|
||||
"creationTimestamp": null,
|
||||
"labels": {
|
||||
"app": "nginx"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"containers": [
|
||||
{
|
||||
"image": "nginx:1.14.2",
|
||||
"name": "nginx",
|
||||
"ports": [
|
||||
{
|
||||
"containerPort": 80,
|
||||
"protocol": "TCP"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}`
|
||||
|
||||
mchJson = `{
|
||||
"apiVersion": "operator.open-cluster-management.io/v1",
|
||||
"kind": "MultiClusterHub",
|
||||
"metadata": {
|
||||
"name": "multiclusterhub",
|
||||
"namespace": "open-cluster-management"
|
||||
},
|
||||
"spec": {
|
||||
"separateCertificateManagement": false
|
||||
}
|
||||
}`
|
||||
)
|
||||
|
||||
var _ = ginkgo.Describe("Agent deploy", func() {
|
||||
var managedClusterName string
|
||||
var err error
|
||||
var manifestWorkName string
|
||||
ginkgo.BeforeEach(func() {
|
||||
suffix := rand.String(5)
|
||||
managedClusterName = fmt.Sprintf("managedcluster-%s", suffix)
|
||||
manifestWorkName = fmt.Sprintf("%s-0", constants.DeployWorkNamePrefix(testAddonImpl.name))
|
||||
|
||||
managedCluster := &clusterv1.ManagedCluster{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: managedClusterName,
|
||||
},
|
||||
Spec: clusterv1.ManagedClusterSpec{
|
||||
HubAcceptsClient: true,
|
||||
},
|
||||
}
|
||||
_, err = hubClusterClient.ClusterV1().ManagedClusters().Create(context.Background(), managedCluster, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: managedClusterName}}
|
||||
_, err = hubKubeClient.CoreV1().Namespaces().Create(context.Background(), ns, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
cma := newClusterManagementAddon(testAddonImpl.name)
|
||||
_, err = hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Create(context.Background(),
|
||||
cma, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
})
|
||||
|
||||
ginkgo.AfterEach(func() {
|
||||
err = hubKubeClient.CoreV1().Namespaces().Delete(context.Background(), managedClusterName, metav1.DeleteOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
err = hubClusterClient.ClusterV1().ManagedClusters().Delete(context.Background(), managedClusterName, metav1.DeleteOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
err = hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Delete(context.Background(),
|
||||
testAddonImpl.name, metav1.DeleteOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
})
|
||||
|
||||
ginkgo.It("Should deploy agent when cma is managed by addon-manager successfully", func() {
|
||||
obj := &unstructured.Unstructured{}
|
||||
err := obj.UnmarshalJSON([]byte(deploymentJson))
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
testAddonImpl.manifests[managedClusterName] = []runtime.Object{obj}
|
||||
testAddonImpl.prober = &agent.HealthProber{
|
||||
Type: agent.HealthProberTypeWork,
|
||||
}
|
||||
|
||||
// Update clustermanagement addon annotattion
|
||||
cma, err := hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Get(context.Background(), testAddonImpl.name, metav1.GetOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
cma.SetAnnotations(map[string]string{addonapiv1alpha1.AddonLifecycleAnnotationKey: addonapiv1alpha1.AddonLifecycleAddonManagerAnnotationValue})
|
||||
_, err = hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Update(context.Background(), cma, metav1.UpdateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
// Create ManagedClusterAddOn
|
||||
addon := &addonapiv1alpha1.ManagedClusterAddOn{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: testAddonImpl.name,
|
||||
},
|
||||
Spec: addonapiv1alpha1.ManagedClusterAddOnSpec{
|
||||
InstallNamespace: "default",
|
||||
},
|
||||
}
|
||||
_, err = hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Create(context.Background(), addon, metav1.CreateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
gomega.Eventually(func() error {
|
||||
work, err := hubWorkClient.WorkV1().ManifestWorks(managedClusterName).Get(context.Background(), manifestWorkName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(work.Spec.Workload.Manifests) != 1 {
|
||||
return fmt.Errorf("Unexpected number of work manifests")
|
||||
}
|
||||
|
||||
if apiequality.Semantic.DeepEqual(work.Spec.Workload.Manifests[0].Raw, []byte(deploymentJson)) {
|
||||
return fmt.Errorf("expected manifest is no correct, get %v", work.Spec.Workload.Manifests[0].Raw)
|
||||
}
|
||||
return nil
|
||||
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())
|
||||
|
||||
// Update work status to trigger addon status
|
||||
work, err := hubWorkClient.WorkV1().ManifestWorks(managedClusterName).Get(context.Background(), manifestWorkName, metav1.GetOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
meta.SetStatusCondition(&work.Status.Conditions, metav1.Condition{Type: workapiv1.WorkApplied, Status: metav1.ConditionTrue, Reason: "WorkApplied", ObservedGeneration: work.Generation})
|
||||
_, err = hubWorkClient.WorkV1().ManifestWorks(managedClusterName).UpdateStatus(context.Background(), work, metav1.UpdateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
gomega.Eventually(func() error {
|
||||
addon, err := hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Get(context.Background(), testAddonImpl.name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !meta.IsStatusConditionTrue(addon.Status.Conditions, addonapiv1alpha1.ManagedClusterAddOnManifestApplied) {
|
||||
return fmt.Errorf("Unexpected addon applied condition, %v", addon.Status.Conditions)
|
||||
}
|
||||
if !meta.IsStatusConditionTrue(addon.Status.Conditions, addonapiv1alpha1.ManagedClusterAddOnConditionProgressing) {
|
||||
return fmt.Errorf("Unexpected addon progressing condition, %v", addon.Status.Conditions)
|
||||
}
|
||||
return nil
|
||||
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())
|
||||
|
||||
// update work to available so addon becomes available
|
||||
work, err = hubWorkClient.WorkV1().ManifestWorks(managedClusterName).Get(context.Background(), manifestWorkName, metav1.GetOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
meta.SetStatusCondition(&work.Status.Conditions, metav1.Condition{Type: workapiv1.WorkAvailable, Status: metav1.ConditionTrue, Reason: "WorkAvailable", ObservedGeneration: work.Generation})
|
||||
_, err = hubWorkClient.WorkV1().ManifestWorks(managedClusterName).UpdateStatus(context.Background(), work, metav1.UpdateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
gomega.Eventually(func() error {
|
||||
addon, err := hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Get(context.Background(), testAddonImpl.name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !meta.IsStatusConditionTrue(addon.Status.Conditions, addonapiv1alpha1.ManagedClusterAddOnConditionAvailable) {
|
||||
return fmt.Errorf("Unexpected addon available condition, %v", addon.Status.Conditions)
|
||||
}
|
||||
if !meta.IsStatusConditionFalse(addon.Status.Conditions, addonapiv1alpha1.ManagedClusterAddOnConditionProgressing) {
|
||||
return fmt.Errorf("Unexpected addon progressing condition, %v", addon.Status.Conditions)
|
||||
}
|
||||
return nil
|
||||
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())
|
||||
|
||||
// do nothing if cluster is deleting and addon is not deleted
|
||||
cluster, err := hubClusterClient.ClusterV1().ManagedClusters().Get(context.Background(), managedClusterName, metav1.GetOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
cluster.SetFinalizers([]string{"cluster.open-cluster-management.io/api-resource-cleanup"})
|
||||
_, err = hubClusterClient.ClusterV1().ManagedClusters().Update(context.Background(), cluster, metav1.UpdateOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
err = hubClusterClient.ClusterV1().ManagedClusters().Delete(context.Background(), managedClusterName, metav1.DeleteOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
time.Sleep(5 * time.Second) // wait 5 seconds to sync
|
||||
gomega.Eventually(func() error {
|
||||
_, err = hubWorkClient.WorkV1().ManifestWorks(managedClusterName).Get(context.Background(), manifestWorkName, metav1.GetOptions{})
|
||||
return err
|
||||
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())
|
||||
})
|
||||
|
||||
})
|
||||
242
test/integration/addon/assertion_test.go
Normal file
242
test/integration/addon/assertion_test.go
Normal file
@@ -0,0 +1,242 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
ginkgo "github.com/onsi/ginkgo/v2"
|
||||
"github.com/onsi/gomega"
|
||||
apiequality "k8s.io/apimachinery/pkg/api/equality"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
)
|
||||
|
||||
const addOnDefaultConfigSpecHash = "287d774850847584cc3ebd8b72e2ad3ef8ac6c31803a59324943a7f94054b08a"
|
||||
const addOnTest1ConfigSpecHash = "d76dad0a6448910652950163cc4324e4616ab5143046555c5ad5b003a622ab8d"
|
||||
const addOnTest2ConfigSpecHash = "3f815fe02492288fd235ed9bd881987aebb6f15fd2fa2b37c982525c293679bd"
|
||||
|
||||
var addOnDefaultConfigSpec = addonapiv1alpha1.AddOnDeploymentConfigSpec{
|
||||
CustomizedVariables: []addonapiv1alpha1.CustomizedVariable{
|
||||
{
|
||||
Name: "test",
|
||||
Value: "test",
|
||||
},
|
||||
},
|
||||
}
|
||||
var addOnTest1ConfigSpec = addonapiv1alpha1.AddOnDeploymentConfigSpec{
|
||||
CustomizedVariables: []addonapiv1alpha1.CustomizedVariable{
|
||||
{
|
||||
Name: "test1",
|
||||
Value: "test1",
|
||||
},
|
||||
},
|
||||
}
|
||||
var addOnTest2ConfigSpec = addonapiv1alpha1.AddOnDeploymentConfigSpec{
|
||||
CustomizedVariables: []addonapiv1alpha1.CustomizedVariable{
|
||||
{
|
||||
Name: "test2",
|
||||
Value: "test2",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func createClusterManagementAddOn(name, defaultConfigNamespace, defaultConfigName string) (*addonapiv1alpha1.ClusterManagementAddOn, error) {
|
||||
clusterManagementAddon, err := hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Get(context.Background(), name, metav1.GetOptions{})
|
||||
if errors.IsNotFound(err) {
|
||||
clusterManagementAddon, err = hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Create(
|
||||
context.Background(),
|
||||
&addonapiv1alpha1.ClusterManagementAddOn{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Annotations: map[string]string{
|
||||
addonapiv1alpha1.AddonLifecycleAnnotationKey: addonapiv1alpha1.AddonLifecycleAddonManagerAnnotationValue,
|
||||
},
|
||||
},
|
||||
Spec: addonapiv1alpha1.ClusterManagementAddOnSpec{
|
||||
SupportedConfigs: []addonapiv1alpha1.ConfigMeta{
|
||||
{
|
||||
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
|
||||
Group: addOnDeploymentConfigGVR.Group,
|
||||
Resource: addOnDeploymentConfigGVR.Resource,
|
||||
},
|
||||
DefaultConfig: &addonapiv1alpha1.ConfigReferent{
|
||||
Name: defaultConfigName,
|
||||
Namespace: defaultConfigNamespace,
|
||||
},
|
||||
},
|
||||
},
|
||||
InstallStrategy: addonapiv1alpha1.InstallStrategy{
|
||||
Type: addonapiv1alpha1.AddonInstallStrategyManual,
|
||||
},
|
||||
},
|
||||
},
|
||||
metav1.CreateOptions{},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return clusterManagementAddon, nil
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return clusterManagementAddon, nil
|
||||
}
|
||||
|
||||
func updateClusterManagementAddOn(ctx context.Context, new *addonapiv1alpha1.ClusterManagementAddOn) {
|
||||
gomega.Eventually(func() bool {
|
||||
old, err := hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Get(context.Background(), new.Name, metav1.GetOptions{})
|
||||
old.Spec = new.Spec
|
||||
old.Annotations = new.Annotations
|
||||
_, err = hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Update(context.Background(), old, metav1.UpdateOptions{})
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}, eventuallyTimeout, eventuallyInterval).Should(gomega.BeTrue())
|
||||
}
|
||||
|
||||
func updateManagedClusterAddOnStatus(ctx context.Context, new *addonapiv1alpha1.ManagedClusterAddOn) {
|
||||
gomega.Eventually(func() bool {
|
||||
old, err := hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(new.Namespace).Get(context.Background(), new.Name, metav1.GetOptions{})
|
||||
old.Status = new.Status
|
||||
_, err = hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(old.Namespace).UpdateStatus(context.Background(), old, metav1.UpdateOptions{})
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}, eventuallyTimeout, eventuallyInterval).Should(gomega.BeTrue())
|
||||
}
|
||||
|
||||
func assertClusterManagementAddOnDefaultConfigReferences(name string, expect ...addonapiv1alpha1.DefaultConfigReference) {
|
||||
ginkgo.By(fmt.Sprintf("Check ClusterManagementAddOn %s DefaultConfigReferences", name))
|
||||
|
||||
gomega.Eventually(func() error {
|
||||
actual, err := hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Get(context.Background(), name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(actual.Status.DefaultConfigReferences) != len(expect) {
|
||||
return fmt.Errorf("Expected %v default config reference, actual: %v", len(expect), len(actual.Status.DefaultConfigReferences))
|
||||
}
|
||||
|
||||
for i, e := range expect {
|
||||
actualConfigReference := actual.Status.DefaultConfigReferences[i]
|
||||
|
||||
if !apiequality.Semantic.DeepEqual(actualConfigReference, e) {
|
||||
return fmt.Errorf("Expected default config is %v, actual: %v", e, actualConfigReference)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())
|
||||
}
|
||||
|
||||
func assertClusterManagementAddOnInstallProgression(name string, expect ...addonapiv1alpha1.InstallProgression) {
|
||||
ginkgo.By(fmt.Sprintf("Check ClusterManagementAddOn %s InstallProgression", name))
|
||||
|
||||
gomega.Eventually(func() error {
|
||||
actual, err := hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Get(context.Background(), name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(actual.Status.InstallProgressions) != len(expect) {
|
||||
return fmt.Errorf("Expected %v install progression, actual: %v", len(expect), len(actual.Status.InstallProgressions))
|
||||
}
|
||||
|
||||
for i, e := range expect {
|
||||
actualInstallProgression := actual.Status.InstallProgressions[i]
|
||||
|
||||
if !apiequality.Semantic.DeepEqual(actualInstallProgression.ConfigReferences, e.ConfigReferences) {
|
||||
return fmt.Errorf("Expected InstallProgression.ConfigReferences is %v, actual: %v", e.ConfigReferences, actualInstallProgression.ConfigReferences)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())
|
||||
}
|
||||
|
||||
func assertClusterManagementAddOnConditions(name string, expect ...metav1.Condition) {
|
||||
ginkgo.By(fmt.Sprintf("Check ClusterManagementAddOn %s Conditions", name))
|
||||
|
||||
gomega.Eventually(func() error {
|
||||
actual, err := hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Get(context.Background(), name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i, ec := range expect {
|
||||
cond := meta.FindStatusCondition(actual.Status.InstallProgressions[i].Conditions, ec.Type)
|
||||
if cond == nil ||
|
||||
cond.Status != ec.Status ||
|
||||
cond.Reason != ec.Reason ||
|
||||
cond.Message != ec.Message {
|
||||
return fmt.Errorf("Expected cma progressing condition is %v, actual: %v", ec, cond)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())
|
||||
}
|
||||
|
||||
func assertManagedClusterAddOnConfigReferences(name, namespace string, expect ...addonapiv1alpha1.ConfigReference) {
|
||||
ginkgo.By(fmt.Sprintf("Check ManagedClusterAddOn %s/%s ConfigReferences", namespace, name))
|
||||
|
||||
gomega.Eventually(func() error {
|
||||
actual, err := hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(namespace).Get(context.Background(), name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(actual.Status.ConfigReferences) != len(expect) {
|
||||
return fmt.Errorf("Expected %v config reference, actual: %v", len(expect), len(actual.Status.ConfigReferences))
|
||||
}
|
||||
|
||||
for i, e := range expect {
|
||||
actualConfigReference := actual.Status.ConfigReferences[i]
|
||||
|
||||
if !apiequality.Semantic.DeepEqual(actualConfigReference, e) {
|
||||
return fmt.Errorf("Expected mca config reference is %v %v %v, actual: %v %v %v",
|
||||
e.DesiredConfig,
|
||||
e.LastAppliedConfig,
|
||||
e.LastObservedGeneration,
|
||||
actualConfigReference.DesiredConfig,
|
||||
actualConfigReference.LastAppliedConfig,
|
||||
actualConfigReference.LastObservedGeneration,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())
|
||||
}
|
||||
|
||||
func assertManagedClusterAddOnConditions(name, namespace string, expect ...metav1.Condition) {
|
||||
ginkgo.By(fmt.Sprintf("Check ManagedClusterAddOn %s/%s Conditions", namespace, name))
|
||||
|
||||
gomega.Eventually(func() error {
|
||||
actual, err := hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(namespace).Get(context.Background(), name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, ec := range expect {
|
||||
cond := meta.FindStatusCondition(actual.Status.Conditions, ec.Type)
|
||||
if cond == nil ||
|
||||
cond.Status != ec.Status ||
|
||||
cond.Reason != ec.Reason ||
|
||||
cond.Message != ec.Message {
|
||||
return fmt.Errorf("Expected addon progressing condition is %v, actual: %v", ec, cond)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())
|
||||
}
|
||||
183
test/integration/addon/suite_test.go
Normal file
183
test/integration/addon/suite_test.go
Normal file
@@ -0,0 +1,183 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
ginkgo "github.com/onsi/ginkgo/v2"
|
||||
"github.com/onsi/gomega"
|
||||
"github.com/openshift/library-go/pkg/controller/controllercmd"
|
||||
certificatesv1 "k8s.io/api/certificates/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"sigs.k8s.io/controller-runtime/pkg/envtest"
|
||||
|
||||
"open-cluster-management.io/addon-framework/pkg/addonmanager"
|
||||
"open-cluster-management.io/addon-framework/pkg/agent"
|
||||
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
||||
addonv1alpha1client "open-cluster-management.io/api/client/addon/clientset/versioned"
|
||||
clusterv1client "open-cluster-management.io/api/client/cluster/clientset/versioned"
|
||||
workclientset "open-cluster-management.io/api/client/work/clientset/versioned"
|
||||
clusterv1 "open-cluster-management.io/api/cluster/v1"
|
||||
|
||||
"open-cluster-management.io/ocm/pkg/addon"
|
||||
"open-cluster-management.io/ocm/test/integration/util"
|
||||
)
|
||||
|
||||
const (
|
||||
eventuallyTimeout = 30 // seconds
|
||||
eventuallyInterval = 1 // seconds
|
||||
)
|
||||
|
||||
var addOnDeploymentConfigGVR = schema.GroupVersionResource{
|
||||
Group: "addon.open-cluster-management.io",
|
||||
Version: "v1alpha1",
|
||||
Resource: "addondeploymentconfigs",
|
||||
}
|
||||
|
||||
var testEnv *envtest.Environment
|
||||
var hubWorkClient workclientset.Interface
|
||||
var hubClusterClient clusterv1client.Interface
|
||||
var hubAddonClient addonv1alpha1client.Interface
|
||||
var hubKubeClient kubernetes.Interface
|
||||
var testAddonImpl *testAddon
|
||||
var testAddOnConfigsImpl *testAddon
|
||||
|
||||
var cancel context.CancelFunc
|
||||
var mgrContext context.Context
|
||||
var addonManager addonmanager.AddonManager
|
||||
|
||||
func TestIntegration(t *testing.T) {
|
||||
gomega.RegisterFailHandler(ginkgo.Fail)
|
||||
ginkgo.RunSpecs(t, "Integration Suite")
|
||||
}
|
||||
|
||||
var _ = ginkgo.BeforeSuite(func() {
|
||||
ginkgo.By("bootstrapping test environment")
|
||||
|
||||
// start a kube-apiserver
|
||||
testEnv = &envtest.Environment{
|
||||
ErrorIfCRDPathMissing: true,
|
||||
CRDDirectoryPaths: []string{
|
||||
filepath.Join(".", "vendor", "open-cluster-management.io", "api", "work", "v1", "0000_00_work.open-cluster-management.io_manifestworks.crd.yaml"),
|
||||
filepath.Join(".", "vendor", "open-cluster-management.io", "api", "cluster", "v1"),
|
||||
filepath.Join(".", "vendor", "open-cluster-management.io", "api", "cluster", "v1beta1"),
|
||||
filepath.Join(".", "vendor", "open-cluster-management.io", "api", "addon", "v1alpha1"),
|
||||
},
|
||||
}
|
||||
|
||||
cfg, err := testEnv.Start()
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
gomega.Expect(cfg).ToNot(gomega.BeNil())
|
||||
|
||||
hubWorkClient, err = workclientset.NewForConfig(cfg)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
hubClusterClient, err = clusterv1client.NewForConfig(cfg)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
hubAddonClient, err = addonv1alpha1client.NewForConfig(cfg)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
hubKubeClient, err = kubernetes.NewForConfig(cfg)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
|
||||
testAddonImpl = &testAddon{
|
||||
name: "test",
|
||||
manifests: map[string][]runtime.Object{},
|
||||
registrations: map[string][]addonapiv1alpha1.RegistrationConfig{},
|
||||
}
|
||||
|
||||
testAddOnConfigsImpl = &testAddon{
|
||||
name: "test-addon-configs",
|
||||
manifests: map[string][]runtime.Object{},
|
||||
registrations: map[string][]addonapiv1alpha1.RegistrationConfig{},
|
||||
supportedConfigGVRs: []schema.GroupVersionResource{addOnDeploymentConfigGVR},
|
||||
}
|
||||
|
||||
mgrContext, cancel = context.WithCancel(context.TODO())
|
||||
// start hub controller
|
||||
go func() {
|
||||
addonManager, err = addonmanager.New(cfg)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
|
||||
err = addonManager.AddAgent(testAddonImpl)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
err = addonManager.AddAgent(testAddOnConfigsImpl)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
|
||||
err = addonManager.Start(mgrContext)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
|
||||
err = addon.RunManager(mgrContext, &controllercmd.ControllerContext{
|
||||
KubeConfig: cfg,
|
||||
EventRecorder: util.NewIntegrationTestEventRecorder("integration"),
|
||||
})
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
}()
|
||||
|
||||
})
|
||||
|
||||
var _ = ginkgo.AfterSuite(func() {
|
||||
ginkgo.By("tearing down the test environment")
|
||||
|
||||
cancel()
|
||||
err := testEnv.Stop()
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
})
|
||||
|
||||
type testAddon struct {
|
||||
name string
|
||||
manifests map[string][]runtime.Object
|
||||
registrations map[string][]addonapiv1alpha1.RegistrationConfig
|
||||
approveCSR bool
|
||||
cert []byte
|
||||
prober *agent.HealthProber
|
||||
installStrategy *agent.InstallStrategy
|
||||
hostedModeEnabled bool
|
||||
supportedConfigGVRs []schema.GroupVersionResource
|
||||
}
|
||||
|
||||
func (t *testAddon) Manifests(cluster *clusterv1.ManagedCluster, addon *addonapiv1alpha1.ManagedClusterAddOn) ([]runtime.Object, error) {
|
||||
return t.manifests[cluster.Name], nil
|
||||
}
|
||||
|
||||
func (t *testAddon) GetAgentAddonOptions() agent.AgentAddonOptions {
|
||||
option := agent.AgentAddonOptions{
|
||||
AddonName: t.name,
|
||||
HealthProber: t.prober,
|
||||
InstallStrategy: t.installStrategy,
|
||||
HostedModeEnabled: t.hostedModeEnabled,
|
||||
SupportedConfigGVRs: t.supportedConfigGVRs,
|
||||
}
|
||||
|
||||
if len(t.registrations) > 0 {
|
||||
option.Registration = &agent.RegistrationOption{
|
||||
CSRConfigurations: func(cluster *clusterv1.ManagedCluster) []addonapiv1alpha1.RegistrationConfig {
|
||||
return t.registrations[cluster.Name]
|
||||
},
|
||||
CSRApproveCheck: func(cluster *clusterv1.ManagedCluster, addon *addonapiv1alpha1.ManagedClusterAddOn, csr *certificatesv1.CertificateSigningRequest) bool {
|
||||
return t.approveCSR
|
||||
},
|
||||
CSRSign: func(csr *certificatesv1.CertificateSigningRequest) []byte {
|
||||
return t.cert
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return option
|
||||
}
|
||||
|
||||
func newClusterManagementAddon(name string) *addonapiv1alpha1.ClusterManagementAddOn {
|
||||
return &addonapiv1alpha1.ClusterManagementAddOn{
|
||||
TypeMeta: metav1.TypeMeta{},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
},
|
||||
Spec: addonapiv1alpha1.ClusterManagementAddOnSpec{
|
||||
InstallStrategy: addonapiv1alpha1.InstallStrategy{
|
||||
Type: addonapiv1alpha1.AddonInstallStrategyManual,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
2
vendor/github.com/BurntSushi/toml/.gitignore
generated
vendored
Normal file
2
vendor/github.com/BurntSushi/toml/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/toml.test
|
||||
/toml-test
|
||||
21
vendor/github.com/BurntSushi/toml/COPYING
generated
vendored
Normal file
21
vendor/github.com/BurntSushi/toml/COPYING
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 TOML authors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
120
vendor/github.com/BurntSushi/toml/README.md
generated
vendored
Normal file
120
vendor/github.com/BurntSushi/toml/README.md
generated
vendored
Normal file
@@ -0,0 +1,120 @@
|
||||
TOML stands for Tom's Obvious, Minimal Language. This Go package provides a
|
||||
reflection interface similar to Go's standard library `json` and `xml` packages.
|
||||
|
||||
Compatible with TOML version [v1.0.0](https://toml.io/en/v1.0.0).
|
||||
|
||||
Documentation: https://godocs.io/github.com/BurntSushi/toml
|
||||
|
||||
See the [releases page](https://github.com/BurntSushi/toml/releases) for a
|
||||
changelog; this information is also in the git tag annotations (e.g. `git show
|
||||
v0.4.0`).
|
||||
|
||||
This library requires Go 1.13 or newer; add it to your go.mod with:
|
||||
|
||||
% go get github.com/BurntSushi/toml@latest
|
||||
|
||||
It also comes with a TOML validator CLI tool:
|
||||
|
||||
% go install github.com/BurntSushi/toml/cmd/tomlv@latest
|
||||
% tomlv some-toml-file.toml
|
||||
|
||||
### Examples
|
||||
For the simplest example, consider some TOML file as just a list of keys and
|
||||
values:
|
||||
|
||||
```toml
|
||||
Age = 25
|
||||
Cats = [ "Cauchy", "Plato" ]
|
||||
Pi = 3.14
|
||||
Perfection = [ 6, 28, 496, 8128 ]
|
||||
DOB = 1987-07-05T05:45:00Z
|
||||
```
|
||||
|
||||
Which can be decoded with:
|
||||
|
||||
```go
|
||||
type Config struct {
|
||||
Age int
|
||||
Cats []string
|
||||
Pi float64
|
||||
Perfection []int
|
||||
DOB time.Time
|
||||
}
|
||||
|
||||
var conf Config
|
||||
_, err := toml.Decode(tomlData, &conf)
|
||||
```
|
||||
|
||||
You can also use struct tags if your struct field name doesn't map to a TOML key
|
||||
value directly:
|
||||
|
||||
```toml
|
||||
some_key_NAME = "wat"
|
||||
```
|
||||
|
||||
```go
|
||||
type TOML struct {
|
||||
ObscureKey string `toml:"some_key_NAME"`
|
||||
}
|
||||
```
|
||||
|
||||
Beware that like other decoders **only exported fields** are considered when
|
||||
encoding and decoding; private fields are silently ignored.
|
||||
|
||||
### Using the `Marshaler` and `encoding.TextUnmarshaler` interfaces
|
||||
Here's an example that automatically parses values in a `mail.Address`:
|
||||
|
||||
```toml
|
||||
contacts = [
|
||||
"Donald Duck <donald@duckburg.com>",
|
||||
"Scrooge McDuck <scrooge@duckburg.com>",
|
||||
]
|
||||
```
|
||||
|
||||
Can be decoded with:
|
||||
|
||||
```go
|
||||
// Create address type which satisfies the encoding.TextUnmarshaler interface.
|
||||
type address struct {
|
||||
*mail.Address
|
||||
}
|
||||
|
||||
func (a *address) UnmarshalText(text []byte) error {
|
||||
var err error
|
||||
a.Address, err = mail.ParseAddress(string(text))
|
||||
return err
|
||||
}
|
||||
|
||||
// Decode it.
|
||||
func decode() {
|
||||
blob := `
|
||||
contacts = [
|
||||
"Donald Duck <donald@duckburg.com>",
|
||||
"Scrooge McDuck <scrooge@duckburg.com>",
|
||||
]
|
||||
`
|
||||
|
||||
var contacts struct {
|
||||
Contacts []address
|
||||
}
|
||||
|
||||
_, err := toml.Decode(blob, &contacts)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for _, c := range contacts.Contacts {
|
||||
fmt.Printf("%#v\n", c.Address)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// &mail.Address{Name:"Donald Duck", Address:"donald@duckburg.com"}
|
||||
// &mail.Address{Name:"Scrooge McDuck", Address:"scrooge@duckburg.com"}
|
||||
}
|
||||
```
|
||||
|
||||
To target TOML specifically you can implement `UnmarshalTOML` TOML interface in
|
||||
a similar way.
|
||||
|
||||
### More complex usage
|
||||
See the [`_example/`](/_example) directory for a more complex example.
|
||||
602
vendor/github.com/BurntSushi/toml/decode.go
generated
vendored
Normal file
602
vendor/github.com/BurntSushi/toml/decode.go
generated
vendored
Normal file
@@ -0,0 +1,602 @@
|
||||
package toml
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Unmarshaler is the interface implemented by objects that can unmarshal a
|
||||
// TOML description of themselves.
|
||||
type Unmarshaler interface {
|
||||
UnmarshalTOML(interface{}) error
|
||||
}
|
||||
|
||||
// Unmarshal decodes the contents of data in TOML format into a pointer v.
|
||||
//
|
||||
// See [Decoder] for a description of the decoding process.
|
||||
func Unmarshal(data []byte, v interface{}) error {
|
||||
_, err := NewDecoder(bytes.NewReader(data)).Decode(v)
|
||||
return err
|
||||
}
|
||||
|
||||
// Decode the TOML data in to the pointer v.
|
||||
//
|
||||
// See [Decoder] for a description of the decoding process.
|
||||
func Decode(data string, v interface{}) (MetaData, error) {
|
||||
return NewDecoder(strings.NewReader(data)).Decode(v)
|
||||
}
|
||||
|
||||
// DecodeFile reads the contents of a file and decodes it with [Decode].
|
||||
func DecodeFile(path string, v interface{}) (MetaData, error) {
|
||||
fp, err := os.Open(path)
|
||||
if err != nil {
|
||||
return MetaData{}, err
|
||||
}
|
||||
defer fp.Close()
|
||||
return NewDecoder(fp).Decode(v)
|
||||
}
|
||||
|
||||
// Primitive is a TOML value that hasn't been decoded into a Go value.
|
||||
//
|
||||
// This type can be used for any value, which will cause decoding to be delayed.
|
||||
// You can use [PrimitiveDecode] to "manually" decode these values.
|
||||
//
|
||||
// NOTE: The underlying representation of a `Primitive` value is subject to
|
||||
// change. Do not rely on it.
|
||||
//
|
||||
// NOTE: Primitive values are still parsed, so using them will only avoid the
|
||||
// overhead of reflection. They can be useful when you don't know the exact type
|
||||
// of TOML data until runtime.
|
||||
type Primitive struct {
|
||||
undecoded interface{}
|
||||
context Key
|
||||
}
|
||||
|
||||
// The significand precision for float32 and float64 is 24 and 53 bits; this is
|
||||
// the range a natural number can be stored in a float without loss of data.
|
||||
const (
|
||||
maxSafeFloat32Int = 16777215 // 2^24-1
|
||||
maxSafeFloat64Int = int64(9007199254740991) // 2^53-1
|
||||
)
|
||||
|
||||
// Decoder decodes TOML data.
|
||||
//
|
||||
// TOML tables correspond to Go structs or maps; they can be used
|
||||
// interchangeably, but structs offer better type safety.
|
||||
//
|
||||
// TOML table arrays correspond to either a slice of structs or a slice of maps.
|
||||
//
|
||||
// TOML datetimes correspond to [time.Time]. Local datetimes are parsed in the
|
||||
// local timezone.
|
||||
//
|
||||
// [time.Duration] types are treated as nanoseconds if the TOML value is an
|
||||
// integer, or they're parsed with time.ParseDuration() if they're strings.
|
||||
//
|
||||
// All other TOML types (float, string, int, bool and array) correspond to the
|
||||
// obvious Go types.
|
||||
//
|
||||
// An exception to the above rules is if a type implements the TextUnmarshaler
|
||||
// interface, in which case any primitive TOML value (floats, strings, integers,
|
||||
// booleans, datetimes) will be converted to a []byte and given to the value's
|
||||
// UnmarshalText method. See the Unmarshaler example for a demonstration with
|
||||
// email addresses.
|
||||
//
|
||||
// ### Key mapping
|
||||
//
|
||||
// TOML keys can map to either keys in a Go map or field names in a Go struct.
|
||||
// The special `toml` struct tag can be used to map TOML keys to struct fields
|
||||
// that don't match the key name exactly (see the example). A case insensitive
|
||||
// match to struct names will be tried if an exact match can't be found.
|
||||
//
|
||||
// The mapping between TOML values and Go values is loose. That is, there may
|
||||
// exist TOML values that cannot be placed into your representation, and there
|
||||
// may be parts of your representation that do not correspond to TOML values.
|
||||
// This loose mapping can be made stricter by using the IsDefined and/or
|
||||
// Undecoded methods on the MetaData returned.
|
||||
//
|
||||
// This decoder does not handle cyclic types. Decode will not terminate if a
|
||||
// cyclic type is passed.
|
||||
type Decoder struct {
|
||||
r io.Reader
|
||||
}
|
||||
|
||||
// NewDecoder creates a new Decoder.
|
||||
func NewDecoder(r io.Reader) *Decoder {
|
||||
return &Decoder{r: r}
|
||||
}
|
||||
|
||||
var (
|
||||
unmarshalToml = reflect.TypeOf((*Unmarshaler)(nil)).Elem()
|
||||
unmarshalText = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
|
||||
primitiveType = reflect.TypeOf((*Primitive)(nil)).Elem()
|
||||
)
|
||||
|
||||
// Decode TOML data in to the pointer `v`.
|
||||
func (dec *Decoder) Decode(v interface{}) (MetaData, error) {
|
||||
rv := reflect.ValueOf(v)
|
||||
if rv.Kind() != reflect.Ptr {
|
||||
s := "%q"
|
||||
if reflect.TypeOf(v) == nil {
|
||||
s = "%v"
|
||||
}
|
||||
|
||||
return MetaData{}, fmt.Errorf("toml: cannot decode to non-pointer "+s, reflect.TypeOf(v))
|
||||
}
|
||||
if rv.IsNil() {
|
||||
return MetaData{}, fmt.Errorf("toml: cannot decode to nil value of %q", reflect.TypeOf(v))
|
||||
}
|
||||
|
||||
// Check if this is a supported type: struct, map, interface{}, or something
|
||||
// that implements UnmarshalTOML or UnmarshalText.
|
||||
rv = indirect(rv)
|
||||
rt := rv.Type()
|
||||
if rv.Kind() != reflect.Struct && rv.Kind() != reflect.Map &&
|
||||
!(rv.Kind() == reflect.Interface && rv.NumMethod() == 0) &&
|
||||
!rt.Implements(unmarshalToml) && !rt.Implements(unmarshalText) {
|
||||
return MetaData{}, fmt.Errorf("toml: cannot decode to type %s", rt)
|
||||
}
|
||||
|
||||
// TODO: parser should read from io.Reader? Or at the very least, make it
|
||||
// read from []byte rather than string
|
||||
data, err := ioutil.ReadAll(dec.r)
|
||||
if err != nil {
|
||||
return MetaData{}, err
|
||||
}
|
||||
|
||||
p, err := parse(string(data))
|
||||
if err != nil {
|
||||
return MetaData{}, err
|
||||
}
|
||||
|
||||
md := MetaData{
|
||||
mapping: p.mapping,
|
||||
keyInfo: p.keyInfo,
|
||||
keys: p.ordered,
|
||||
decoded: make(map[string]struct{}, len(p.ordered)),
|
||||
context: nil,
|
||||
data: data,
|
||||
}
|
||||
return md, md.unify(p.mapping, rv)
|
||||
}
|
||||
|
||||
// PrimitiveDecode is just like the other Decode* functions, except it decodes a
|
||||
// TOML value that has already been parsed. Valid primitive values can *only* be
|
||||
// obtained from values filled by the decoder functions, including this method.
|
||||
// (i.e., v may contain more [Primitive] values.)
|
||||
//
|
||||
// Meta data for primitive values is included in the meta data returned by the
|
||||
// Decode* functions with one exception: keys returned by the Undecoded method
|
||||
// will only reflect keys that were decoded. Namely, any keys hidden behind a
|
||||
// Primitive will be considered undecoded. Executing this method will update the
|
||||
// undecoded keys in the meta data. (See the example.)
|
||||
func (md *MetaData) PrimitiveDecode(primValue Primitive, v interface{}) error {
|
||||
md.context = primValue.context
|
||||
defer func() { md.context = nil }()
|
||||
return md.unify(primValue.undecoded, rvalue(v))
|
||||
}
|
||||
|
||||
// unify performs a sort of type unification based on the structure of `rv`,
|
||||
// which is the client representation.
|
||||
//
|
||||
// Any type mismatch produces an error. Finding a type that we don't know
|
||||
// how to handle produces an unsupported type error.
|
||||
func (md *MetaData) unify(data interface{}, rv reflect.Value) error {
|
||||
// Special case. Look for a `Primitive` value.
|
||||
// TODO: #76 would make this superfluous after implemented.
|
||||
if rv.Type() == primitiveType {
|
||||
// Save the undecoded data and the key context into the primitive
|
||||
// value.
|
||||
context := make(Key, len(md.context))
|
||||
copy(context, md.context)
|
||||
rv.Set(reflect.ValueOf(Primitive{
|
||||
undecoded: data,
|
||||
context: context,
|
||||
}))
|
||||
return nil
|
||||
}
|
||||
|
||||
rvi := rv.Interface()
|
||||
if v, ok := rvi.(Unmarshaler); ok {
|
||||
return v.UnmarshalTOML(data)
|
||||
}
|
||||
if v, ok := rvi.(encoding.TextUnmarshaler); ok {
|
||||
return md.unifyText(data, v)
|
||||
}
|
||||
|
||||
// TODO:
|
||||
// The behavior here is incorrect whenever a Go type satisfies the
|
||||
// encoding.TextUnmarshaler interface but also corresponds to a TOML hash or
|
||||
// array. In particular, the unmarshaler should only be applied to primitive
|
||||
// TOML values. But at this point, it will be applied to all kinds of values
|
||||
// and produce an incorrect error whenever those values are hashes or arrays
|
||||
// (including arrays of tables).
|
||||
|
||||
k := rv.Kind()
|
||||
|
||||
if k >= reflect.Int && k <= reflect.Uint64 {
|
||||
return md.unifyInt(data, rv)
|
||||
}
|
||||
switch k {
|
||||
case reflect.Ptr:
|
||||
elem := reflect.New(rv.Type().Elem())
|
||||
err := md.unify(data, reflect.Indirect(elem))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rv.Set(elem)
|
||||
return nil
|
||||
case reflect.Struct:
|
||||
return md.unifyStruct(data, rv)
|
||||
case reflect.Map:
|
||||
return md.unifyMap(data, rv)
|
||||
case reflect.Array:
|
||||
return md.unifyArray(data, rv)
|
||||
case reflect.Slice:
|
||||
return md.unifySlice(data, rv)
|
||||
case reflect.String:
|
||||
return md.unifyString(data, rv)
|
||||
case reflect.Bool:
|
||||
return md.unifyBool(data, rv)
|
||||
case reflect.Interface:
|
||||
if rv.NumMethod() > 0 { // Only support empty interfaces are supported.
|
||||
return md.e("unsupported type %s", rv.Type())
|
||||
}
|
||||
return md.unifyAnything(data, rv)
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return md.unifyFloat64(data, rv)
|
||||
}
|
||||
return md.e("unsupported type %s", rv.Kind())
|
||||
}
|
||||
|
||||
func (md *MetaData) unifyStruct(mapping interface{}, rv reflect.Value) error {
|
||||
tmap, ok := mapping.(map[string]interface{})
|
||||
if !ok {
|
||||
if mapping == nil {
|
||||
return nil
|
||||
}
|
||||
return md.e("type mismatch for %s: expected table but found %T",
|
||||
rv.Type().String(), mapping)
|
||||
}
|
||||
|
||||
for key, datum := range tmap {
|
||||
var f *field
|
||||
fields := cachedTypeFields(rv.Type())
|
||||
for i := range fields {
|
||||
ff := &fields[i]
|
||||
if ff.name == key {
|
||||
f = ff
|
||||
break
|
||||
}
|
||||
if f == nil && strings.EqualFold(ff.name, key) {
|
||||
f = ff
|
||||
}
|
||||
}
|
||||
if f != nil {
|
||||
subv := rv
|
||||
for _, i := range f.index {
|
||||
subv = indirect(subv.Field(i))
|
||||
}
|
||||
|
||||
if isUnifiable(subv) {
|
||||
md.decoded[md.context.add(key).String()] = struct{}{}
|
||||
md.context = append(md.context, key)
|
||||
|
||||
err := md.unify(datum, subv)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
md.context = md.context[0 : len(md.context)-1]
|
||||
} else if f.name != "" {
|
||||
return md.e("cannot write unexported field %s.%s", rv.Type().String(), f.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (md *MetaData) unifyMap(mapping interface{}, rv reflect.Value) error {
|
||||
keyType := rv.Type().Key().Kind()
|
||||
if keyType != reflect.String && keyType != reflect.Interface {
|
||||
return fmt.Errorf("toml: cannot decode to a map with non-string key type (%s in %q)",
|
||||
keyType, rv.Type())
|
||||
}
|
||||
|
||||
tmap, ok := mapping.(map[string]interface{})
|
||||
if !ok {
|
||||
if tmap == nil {
|
||||
return nil
|
||||
}
|
||||
return md.badtype("map", mapping)
|
||||
}
|
||||
if rv.IsNil() {
|
||||
rv.Set(reflect.MakeMap(rv.Type()))
|
||||
}
|
||||
for k, v := range tmap {
|
||||
md.decoded[md.context.add(k).String()] = struct{}{}
|
||||
md.context = append(md.context, k)
|
||||
|
||||
rvval := reflect.Indirect(reflect.New(rv.Type().Elem()))
|
||||
|
||||
err := md.unify(v, indirect(rvval))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
md.context = md.context[0 : len(md.context)-1]
|
||||
|
||||
rvkey := indirect(reflect.New(rv.Type().Key()))
|
||||
|
||||
switch keyType {
|
||||
case reflect.Interface:
|
||||
rvkey.Set(reflect.ValueOf(k))
|
||||
case reflect.String:
|
||||
rvkey.SetString(k)
|
||||
}
|
||||
|
||||
rv.SetMapIndex(rvkey, rvval)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (md *MetaData) unifyArray(data interface{}, rv reflect.Value) error {
|
||||
datav := reflect.ValueOf(data)
|
||||
if datav.Kind() != reflect.Slice {
|
||||
if !datav.IsValid() {
|
||||
return nil
|
||||
}
|
||||
return md.badtype("slice", data)
|
||||
}
|
||||
if l := datav.Len(); l != rv.Len() {
|
||||
return md.e("expected array length %d; got TOML array of length %d", rv.Len(), l)
|
||||
}
|
||||
return md.unifySliceArray(datav, rv)
|
||||
}
|
||||
|
||||
func (md *MetaData) unifySlice(data interface{}, rv reflect.Value) error {
|
||||
datav := reflect.ValueOf(data)
|
||||
if datav.Kind() != reflect.Slice {
|
||||
if !datav.IsValid() {
|
||||
return nil
|
||||
}
|
||||
return md.badtype("slice", data)
|
||||
}
|
||||
n := datav.Len()
|
||||
if rv.IsNil() || rv.Cap() < n {
|
||||
rv.Set(reflect.MakeSlice(rv.Type(), n, n))
|
||||
}
|
||||
rv.SetLen(n)
|
||||
return md.unifySliceArray(datav, rv)
|
||||
}
|
||||
|
||||
func (md *MetaData) unifySliceArray(data, rv reflect.Value) error {
|
||||
l := data.Len()
|
||||
for i := 0; i < l; i++ {
|
||||
err := md.unify(data.Index(i).Interface(), indirect(rv.Index(i)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (md *MetaData) unifyString(data interface{}, rv reflect.Value) error {
|
||||
_, ok := rv.Interface().(json.Number)
|
||||
if ok {
|
||||
if i, ok := data.(int64); ok {
|
||||
rv.SetString(strconv.FormatInt(i, 10))
|
||||
} else if f, ok := data.(float64); ok {
|
||||
rv.SetString(strconv.FormatFloat(f, 'f', -1, 64))
|
||||
} else {
|
||||
return md.badtype("string", data)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if s, ok := data.(string); ok {
|
||||
rv.SetString(s)
|
||||
return nil
|
||||
}
|
||||
return md.badtype("string", data)
|
||||
}
|
||||
|
||||
func (md *MetaData) unifyFloat64(data interface{}, rv reflect.Value) error {
|
||||
rvk := rv.Kind()
|
||||
|
||||
if num, ok := data.(float64); ok {
|
||||
switch rvk {
|
||||
case reflect.Float32:
|
||||
if num < -math.MaxFloat32 || num > math.MaxFloat32 {
|
||||
return md.parseErr(errParseRange{i: num, size: rvk.String()})
|
||||
}
|
||||
fallthrough
|
||||
case reflect.Float64:
|
||||
rv.SetFloat(num)
|
||||
default:
|
||||
panic("bug")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if num, ok := data.(int64); ok {
|
||||
if (rvk == reflect.Float32 && (num < -maxSafeFloat32Int || num > maxSafeFloat32Int)) ||
|
||||
(rvk == reflect.Float64 && (num < -maxSafeFloat64Int || num > maxSafeFloat64Int)) {
|
||||
return md.parseErr(errParseRange{i: num, size: rvk.String()})
|
||||
}
|
||||
rv.SetFloat(float64(num))
|
||||
return nil
|
||||
}
|
||||
|
||||
return md.badtype("float", data)
|
||||
}
|
||||
|
||||
func (md *MetaData) unifyInt(data interface{}, rv reflect.Value) error {
|
||||
_, ok := rv.Interface().(time.Duration)
|
||||
if ok {
|
||||
// Parse as string duration, and fall back to regular integer parsing
|
||||
// (as nanosecond) if this is not a string.
|
||||
if s, ok := data.(string); ok {
|
||||
dur, err := time.ParseDuration(s)
|
||||
if err != nil {
|
||||
return md.parseErr(errParseDuration{s})
|
||||
}
|
||||
rv.SetInt(int64(dur))
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
num, ok := data.(int64)
|
||||
if !ok {
|
||||
return md.badtype("integer", data)
|
||||
}
|
||||
|
||||
rvk := rv.Kind()
|
||||
switch {
|
||||
case rvk >= reflect.Int && rvk <= reflect.Int64:
|
||||
if (rvk == reflect.Int8 && (num < math.MinInt8 || num > math.MaxInt8)) ||
|
||||
(rvk == reflect.Int16 && (num < math.MinInt16 || num > math.MaxInt16)) ||
|
||||
(rvk == reflect.Int32 && (num < math.MinInt32 || num > math.MaxInt32)) {
|
||||
return md.parseErr(errParseRange{i: num, size: rvk.String()})
|
||||
}
|
||||
rv.SetInt(num)
|
||||
case rvk >= reflect.Uint && rvk <= reflect.Uint64:
|
||||
unum := uint64(num)
|
||||
if rvk == reflect.Uint8 && (num < 0 || unum > math.MaxUint8) ||
|
||||
rvk == reflect.Uint16 && (num < 0 || unum > math.MaxUint16) ||
|
||||
rvk == reflect.Uint32 && (num < 0 || unum > math.MaxUint32) {
|
||||
return md.parseErr(errParseRange{i: num, size: rvk.String()})
|
||||
}
|
||||
rv.SetUint(unum)
|
||||
default:
|
||||
panic("unreachable")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (md *MetaData) unifyBool(data interface{}, rv reflect.Value) error {
|
||||
if b, ok := data.(bool); ok {
|
||||
rv.SetBool(b)
|
||||
return nil
|
||||
}
|
||||
return md.badtype("boolean", data)
|
||||
}
|
||||
|
||||
func (md *MetaData) unifyAnything(data interface{}, rv reflect.Value) error {
|
||||
rv.Set(reflect.ValueOf(data))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (md *MetaData) unifyText(data interface{}, v encoding.TextUnmarshaler) error {
|
||||
var s string
|
||||
switch sdata := data.(type) {
|
||||
case Marshaler:
|
||||
text, err := sdata.MarshalTOML()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s = string(text)
|
||||
case encoding.TextMarshaler:
|
||||
text, err := sdata.MarshalText()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s = string(text)
|
||||
case fmt.Stringer:
|
||||
s = sdata.String()
|
||||
case string:
|
||||
s = sdata
|
||||
case bool:
|
||||
s = fmt.Sprintf("%v", sdata)
|
||||
case int64:
|
||||
s = fmt.Sprintf("%d", sdata)
|
||||
case float64:
|
||||
s = fmt.Sprintf("%f", sdata)
|
||||
default:
|
||||
return md.badtype("primitive (string-like)", data)
|
||||
}
|
||||
if err := v.UnmarshalText([]byte(s)); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (md *MetaData) badtype(dst string, data interface{}) error {
|
||||
return md.e("incompatible types: TOML value has type %T; destination has type %s", data, dst)
|
||||
}
|
||||
|
||||
func (md *MetaData) parseErr(err error) error {
|
||||
k := md.context.String()
|
||||
return ParseError{
|
||||
LastKey: k,
|
||||
Position: md.keyInfo[k].pos,
|
||||
Line: md.keyInfo[k].pos.Line,
|
||||
err: err,
|
||||
input: string(md.data),
|
||||
}
|
||||
}
|
||||
|
||||
func (md *MetaData) e(format string, args ...interface{}) error {
|
||||
f := "toml: "
|
||||
if len(md.context) > 0 {
|
||||
f = fmt.Sprintf("toml: (last key %q): ", md.context)
|
||||
p := md.keyInfo[md.context.String()].pos
|
||||
if p.Line > 0 {
|
||||
f = fmt.Sprintf("toml: line %d (last key %q): ", p.Line, md.context)
|
||||
}
|
||||
}
|
||||
return fmt.Errorf(f+format, args...)
|
||||
}
|
||||
|
||||
// rvalue returns a reflect.Value of `v`. All pointers are resolved.
|
||||
func rvalue(v interface{}) reflect.Value {
|
||||
return indirect(reflect.ValueOf(v))
|
||||
}
|
||||
|
||||
// indirect returns the value pointed to by a pointer.
|
||||
//
|
||||
// Pointers are followed until the value is not a pointer. New values are
|
||||
// allocated for each nil pointer.
|
||||
//
|
||||
// An exception to this rule is if the value satisfies an interface of interest
|
||||
// to us (like encoding.TextUnmarshaler).
|
||||
func indirect(v reflect.Value) reflect.Value {
|
||||
if v.Kind() != reflect.Ptr {
|
||||
if v.CanSet() {
|
||||
pv := v.Addr()
|
||||
pvi := pv.Interface()
|
||||
if _, ok := pvi.(encoding.TextUnmarshaler); ok {
|
||||
return pv
|
||||
}
|
||||
if _, ok := pvi.(Unmarshaler); ok {
|
||||
return pv
|
||||
}
|
||||
}
|
||||
return v
|
||||
}
|
||||
if v.IsNil() {
|
||||
v.Set(reflect.New(v.Type().Elem()))
|
||||
}
|
||||
return indirect(reflect.Indirect(v))
|
||||
}
|
||||
|
||||
func isUnifiable(rv reflect.Value) bool {
|
||||
if rv.CanSet() {
|
||||
return true
|
||||
}
|
||||
rvi := rv.Interface()
|
||||
if _, ok := rvi.(encoding.TextUnmarshaler); ok {
|
||||
return true
|
||||
}
|
||||
if _, ok := rvi.(Unmarshaler); ok {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
19
vendor/github.com/BurntSushi/toml/decode_go116.go
generated
vendored
Normal file
19
vendor/github.com/BurntSushi/toml/decode_go116.go
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
//go:build go1.16
|
||||
// +build go1.16
|
||||
|
||||
package toml
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
)
|
||||
|
||||
// DecodeFS reads the contents of a file from [fs.FS] and decodes it with
|
||||
// [Decode].
|
||||
func DecodeFS(fsys fs.FS, path string, v interface{}) (MetaData, error) {
|
||||
fp, err := fsys.Open(path)
|
||||
if err != nil {
|
||||
return MetaData{}, err
|
||||
}
|
||||
defer fp.Close()
|
||||
return NewDecoder(fp).Decode(v)
|
||||
}
|
||||
21
vendor/github.com/BurntSushi/toml/deprecated.go
generated
vendored
Normal file
21
vendor/github.com/BurntSushi/toml/deprecated.go
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
package toml
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
"io"
|
||||
)
|
||||
|
||||
// Deprecated: use encoding.TextMarshaler
|
||||
type TextMarshaler encoding.TextMarshaler
|
||||
|
||||
// Deprecated: use encoding.TextUnmarshaler
|
||||
type TextUnmarshaler encoding.TextUnmarshaler
|
||||
|
||||
// Deprecated: use MetaData.PrimitiveDecode.
|
||||
func PrimitiveDecode(primValue Primitive, v interface{}) error {
|
||||
md := MetaData{decoded: make(map[string]struct{})}
|
||||
return md.unify(primValue.undecoded, rvalue(v))
|
||||
}
|
||||
|
||||
// Deprecated: use NewDecoder(reader).Decode(&value).
|
||||
func DecodeReader(r io.Reader, v interface{}) (MetaData, error) { return NewDecoder(r).Decode(v) }
|
||||
11
vendor/github.com/BurntSushi/toml/doc.go
generated
vendored
Normal file
11
vendor/github.com/BurntSushi/toml/doc.go
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
// Package toml implements decoding and encoding of TOML files.
|
||||
//
|
||||
// This package supports TOML v1.0.0, as specified at https://toml.io
|
||||
//
|
||||
// There is also support for delaying decoding with the Primitive type, and
|
||||
// querying the set of keys in a TOML document with the MetaData type.
|
||||
//
|
||||
// The github.com/BurntSushi/toml/cmd/tomlv package implements a TOML validator,
|
||||
// and can be used to verify if TOML document is valid. It can also be used to
|
||||
// print the type of each key.
|
||||
package toml
|
||||
750
vendor/github.com/BurntSushi/toml/encode.go
generated
vendored
Normal file
750
vendor/github.com/BurntSushi/toml/encode.go
generated
vendored
Normal file
@@ -0,0 +1,750 @@
|
||||
package toml
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/BurntSushi/toml/internal"
|
||||
)
|
||||
|
||||
type tomlEncodeError struct{ error }
|
||||
|
||||
var (
|
||||
errArrayNilElement = errors.New("toml: cannot encode array with nil element")
|
||||
errNonString = errors.New("toml: cannot encode a map with non-string key type")
|
||||
errNoKey = errors.New("toml: top-level values must be Go maps or structs")
|
||||
errAnything = errors.New("") // used in testing
|
||||
)
|
||||
|
||||
var dblQuotedReplacer = strings.NewReplacer(
|
||||
"\"", "\\\"",
|
||||
"\\", "\\\\",
|
||||
"\x00", `\u0000`,
|
||||
"\x01", `\u0001`,
|
||||
"\x02", `\u0002`,
|
||||
"\x03", `\u0003`,
|
||||
"\x04", `\u0004`,
|
||||
"\x05", `\u0005`,
|
||||
"\x06", `\u0006`,
|
||||
"\x07", `\u0007`,
|
||||
"\b", `\b`,
|
||||
"\t", `\t`,
|
||||
"\n", `\n`,
|
||||
"\x0b", `\u000b`,
|
||||
"\f", `\f`,
|
||||
"\r", `\r`,
|
||||
"\x0e", `\u000e`,
|
||||
"\x0f", `\u000f`,
|
||||
"\x10", `\u0010`,
|
||||
"\x11", `\u0011`,
|
||||
"\x12", `\u0012`,
|
||||
"\x13", `\u0013`,
|
||||
"\x14", `\u0014`,
|
||||
"\x15", `\u0015`,
|
||||
"\x16", `\u0016`,
|
||||
"\x17", `\u0017`,
|
||||
"\x18", `\u0018`,
|
||||
"\x19", `\u0019`,
|
||||
"\x1a", `\u001a`,
|
||||
"\x1b", `\u001b`,
|
||||
"\x1c", `\u001c`,
|
||||
"\x1d", `\u001d`,
|
||||
"\x1e", `\u001e`,
|
||||
"\x1f", `\u001f`,
|
||||
"\x7f", `\u007f`,
|
||||
)
|
||||
|
||||
var (
|
||||
marshalToml = reflect.TypeOf((*Marshaler)(nil)).Elem()
|
||||
marshalText = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
|
||||
timeType = reflect.TypeOf((*time.Time)(nil)).Elem()
|
||||
)
|
||||
|
||||
// Marshaler is the interface implemented by types that can marshal themselves
|
||||
// into valid TOML.
|
||||
type Marshaler interface {
|
||||
MarshalTOML() ([]byte, error)
|
||||
}
|
||||
|
||||
// Encoder encodes a Go to a TOML document.
|
||||
//
|
||||
// The mapping between Go values and TOML values should be precisely the same as
|
||||
// for [Decode].
|
||||
//
|
||||
// time.Time is encoded as a RFC 3339 string, and time.Duration as its string
|
||||
// representation.
|
||||
//
|
||||
// The [Marshaler] and [encoding.TextMarshaler] interfaces are supported to
|
||||
// encoding the value as custom TOML.
|
||||
//
|
||||
// If you want to write arbitrary binary data then you will need to use
|
||||
// something like base64 since TOML does not have any binary types.
|
||||
//
|
||||
// When encoding TOML hashes (Go maps or structs), keys without any sub-hashes
|
||||
// are encoded first.
|
||||
//
|
||||
// Go maps will be sorted alphabetically by key for deterministic output.
|
||||
//
|
||||
// The toml struct tag can be used to provide the key name; if omitted the
|
||||
// struct field name will be used. If the "omitempty" option is present the
|
||||
// following value will be skipped:
|
||||
//
|
||||
// - arrays, slices, maps, and string with len of 0
|
||||
// - struct with all zero values
|
||||
// - bool false
|
||||
//
|
||||
// If omitzero is given all int and float types with a value of 0 will be
|
||||
// skipped.
|
||||
//
|
||||
// Encoding Go values without a corresponding TOML representation will return an
|
||||
// error. Examples of this includes maps with non-string keys, slices with nil
|
||||
// elements, embedded non-struct types, and nested slices containing maps or
|
||||
// structs. (e.g. [][]map[string]string is not allowed but []map[string]string
|
||||
// is okay, as is []map[string][]string).
|
||||
//
|
||||
// NOTE: only exported keys are encoded due to the use of reflection. Unexported
|
||||
// keys are silently discarded.
|
||||
type Encoder struct {
|
||||
// String to use for a single indentation level; default is two spaces.
|
||||
Indent string
|
||||
|
||||
w *bufio.Writer
|
||||
hasWritten bool // written any output to w yet?
|
||||
}
|
||||
|
||||
// NewEncoder create a new Encoder.
|
||||
func NewEncoder(w io.Writer) *Encoder {
|
||||
return &Encoder{
|
||||
w: bufio.NewWriter(w),
|
||||
Indent: " ",
|
||||
}
|
||||
}
|
||||
|
||||
// Encode writes a TOML representation of the Go value to the [Encoder]'s writer.
|
||||
//
|
||||
// An error is returned if the value given cannot be encoded to a valid TOML
|
||||
// document.
|
||||
func (enc *Encoder) Encode(v interface{}) error {
|
||||
rv := eindirect(reflect.ValueOf(v))
|
||||
if err := enc.safeEncode(Key([]string{}), rv); err != nil {
|
||||
return err
|
||||
}
|
||||
return enc.w.Flush()
|
||||
}
|
||||
|
||||
func (enc *Encoder) safeEncode(key Key, rv reflect.Value) (err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
if terr, ok := r.(tomlEncodeError); ok {
|
||||
err = terr.error
|
||||
return
|
||||
}
|
||||
panic(r)
|
||||
}
|
||||
}()
|
||||
enc.encode(key, rv)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (enc *Encoder) encode(key Key, rv reflect.Value) {
|
||||
// If we can marshal the type to text, then we use that. This prevents the
|
||||
// encoder for handling these types as generic structs (or whatever the
|
||||
// underlying type of a TextMarshaler is).
|
||||
switch {
|
||||
case isMarshaler(rv):
|
||||
enc.writeKeyValue(key, rv, false)
|
||||
return
|
||||
case rv.Type() == primitiveType: // TODO: #76 would make this superfluous after implemented.
|
||||
enc.encode(key, reflect.ValueOf(rv.Interface().(Primitive).undecoded))
|
||||
return
|
||||
}
|
||||
|
||||
k := rv.Kind()
|
||||
switch k {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
|
||||
reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
|
||||
reflect.Uint64,
|
||||
reflect.Float32, reflect.Float64, reflect.String, reflect.Bool:
|
||||
enc.writeKeyValue(key, rv, false)
|
||||
case reflect.Array, reflect.Slice:
|
||||
if typeEqual(tomlArrayHash, tomlTypeOfGo(rv)) {
|
||||
enc.eArrayOfTables(key, rv)
|
||||
} else {
|
||||
enc.writeKeyValue(key, rv, false)
|
||||
}
|
||||
case reflect.Interface:
|
||||
if rv.IsNil() {
|
||||
return
|
||||
}
|
||||
enc.encode(key, rv.Elem())
|
||||
case reflect.Map:
|
||||
if rv.IsNil() {
|
||||
return
|
||||
}
|
||||
enc.eTable(key, rv)
|
||||
case reflect.Ptr:
|
||||
if rv.IsNil() {
|
||||
return
|
||||
}
|
||||
enc.encode(key, rv.Elem())
|
||||
case reflect.Struct:
|
||||
enc.eTable(key, rv)
|
||||
default:
|
||||
encPanic(fmt.Errorf("unsupported type for key '%s': %s", key, k))
|
||||
}
|
||||
}
|
||||
|
||||
// eElement encodes any value that can be an array element.
|
||||
func (enc *Encoder) eElement(rv reflect.Value) {
|
||||
switch v := rv.Interface().(type) {
|
||||
case time.Time: // Using TextMarshaler adds extra quotes, which we don't want.
|
||||
format := time.RFC3339Nano
|
||||
switch v.Location() {
|
||||
case internal.LocalDatetime:
|
||||
format = "2006-01-02T15:04:05.999999999"
|
||||
case internal.LocalDate:
|
||||
format = "2006-01-02"
|
||||
case internal.LocalTime:
|
||||
format = "15:04:05.999999999"
|
||||
}
|
||||
switch v.Location() {
|
||||
default:
|
||||
enc.wf(v.Format(format))
|
||||
case internal.LocalDatetime, internal.LocalDate, internal.LocalTime:
|
||||
enc.wf(v.In(time.UTC).Format(format))
|
||||
}
|
||||
return
|
||||
case Marshaler:
|
||||
s, err := v.MarshalTOML()
|
||||
if err != nil {
|
||||
encPanic(err)
|
||||
}
|
||||
if s == nil {
|
||||
encPanic(errors.New("MarshalTOML returned nil and no error"))
|
||||
}
|
||||
enc.w.Write(s)
|
||||
return
|
||||
case encoding.TextMarshaler:
|
||||
s, err := v.MarshalText()
|
||||
if err != nil {
|
||||
encPanic(err)
|
||||
}
|
||||
if s == nil {
|
||||
encPanic(errors.New("MarshalText returned nil and no error"))
|
||||
}
|
||||
enc.writeQuoted(string(s))
|
||||
return
|
||||
case time.Duration:
|
||||
enc.writeQuoted(v.String())
|
||||
return
|
||||
case json.Number:
|
||||
n, _ := rv.Interface().(json.Number)
|
||||
|
||||
if n == "" { /// Useful zero value.
|
||||
enc.w.WriteByte('0')
|
||||
return
|
||||
} else if v, err := n.Int64(); err == nil {
|
||||
enc.eElement(reflect.ValueOf(v))
|
||||
return
|
||||
} else if v, err := n.Float64(); err == nil {
|
||||
enc.eElement(reflect.ValueOf(v))
|
||||
return
|
||||
}
|
||||
encPanic(fmt.Errorf("unable to convert %q to int64 or float64", n))
|
||||
}
|
||||
|
||||
switch rv.Kind() {
|
||||
case reflect.Ptr:
|
||||
enc.eElement(rv.Elem())
|
||||
return
|
||||
case reflect.String:
|
||||
enc.writeQuoted(rv.String())
|
||||
case reflect.Bool:
|
||||
enc.wf(strconv.FormatBool(rv.Bool()))
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
enc.wf(strconv.FormatInt(rv.Int(), 10))
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
enc.wf(strconv.FormatUint(rv.Uint(), 10))
|
||||
case reflect.Float32:
|
||||
f := rv.Float()
|
||||
if math.IsNaN(f) {
|
||||
enc.wf("nan")
|
||||
} else if math.IsInf(f, 0) {
|
||||
enc.wf("%cinf", map[bool]byte{true: '-', false: '+'}[math.Signbit(f)])
|
||||
} else {
|
||||
enc.wf(floatAddDecimal(strconv.FormatFloat(f, 'f', -1, 32)))
|
||||
}
|
||||
case reflect.Float64:
|
||||
f := rv.Float()
|
||||
if math.IsNaN(f) {
|
||||
enc.wf("nan")
|
||||
} else if math.IsInf(f, 0) {
|
||||
enc.wf("%cinf", map[bool]byte{true: '-', false: '+'}[math.Signbit(f)])
|
||||
} else {
|
||||
enc.wf(floatAddDecimal(strconv.FormatFloat(f, 'f', -1, 64)))
|
||||
}
|
||||
case reflect.Array, reflect.Slice:
|
||||
enc.eArrayOrSliceElement(rv)
|
||||
case reflect.Struct:
|
||||
enc.eStruct(nil, rv, true)
|
||||
case reflect.Map:
|
||||
enc.eMap(nil, rv, true)
|
||||
case reflect.Interface:
|
||||
enc.eElement(rv.Elem())
|
||||
default:
|
||||
encPanic(fmt.Errorf("unexpected type: %T", rv.Interface()))
|
||||
}
|
||||
}
|
||||
|
||||
// By the TOML spec, all floats must have a decimal with at least one number on
|
||||
// either side.
|
||||
func floatAddDecimal(fstr string) string {
|
||||
if !strings.Contains(fstr, ".") {
|
||||
return fstr + ".0"
|
||||
}
|
||||
return fstr
|
||||
}
|
||||
|
||||
func (enc *Encoder) writeQuoted(s string) {
|
||||
enc.wf("\"%s\"", dblQuotedReplacer.Replace(s))
|
||||
}
|
||||
|
||||
func (enc *Encoder) eArrayOrSliceElement(rv reflect.Value) {
|
||||
length := rv.Len()
|
||||
enc.wf("[")
|
||||
for i := 0; i < length; i++ {
|
||||
elem := eindirect(rv.Index(i))
|
||||
enc.eElement(elem)
|
||||
if i != length-1 {
|
||||
enc.wf(", ")
|
||||
}
|
||||
}
|
||||
enc.wf("]")
|
||||
}
|
||||
|
||||
func (enc *Encoder) eArrayOfTables(key Key, rv reflect.Value) {
|
||||
if len(key) == 0 {
|
||||
encPanic(errNoKey)
|
||||
}
|
||||
for i := 0; i < rv.Len(); i++ {
|
||||
trv := eindirect(rv.Index(i))
|
||||
if isNil(trv) {
|
||||
continue
|
||||
}
|
||||
enc.newline()
|
||||
enc.wf("%s[[%s]]", enc.indentStr(key), key)
|
||||
enc.newline()
|
||||
enc.eMapOrStruct(key, trv, false)
|
||||
}
|
||||
}
|
||||
|
||||
func (enc *Encoder) eTable(key Key, rv reflect.Value) {
|
||||
if len(key) == 1 {
|
||||
// Output an extra newline between top-level tables.
|
||||
// (The newline isn't written if nothing else has been written though.)
|
||||
enc.newline()
|
||||
}
|
||||
if len(key) > 0 {
|
||||
enc.wf("%s[%s]", enc.indentStr(key), key)
|
||||
enc.newline()
|
||||
}
|
||||
enc.eMapOrStruct(key, rv, false)
|
||||
}
|
||||
|
||||
func (enc *Encoder) eMapOrStruct(key Key, rv reflect.Value, inline bool) {
|
||||
switch rv.Kind() {
|
||||
case reflect.Map:
|
||||
enc.eMap(key, rv, inline)
|
||||
case reflect.Struct:
|
||||
enc.eStruct(key, rv, inline)
|
||||
default:
|
||||
// Should never happen?
|
||||
panic("eTable: unhandled reflect.Value Kind: " + rv.Kind().String())
|
||||
}
|
||||
}
|
||||
|
||||
func (enc *Encoder) eMap(key Key, rv reflect.Value, inline bool) {
|
||||
rt := rv.Type()
|
||||
if rt.Key().Kind() != reflect.String {
|
||||
encPanic(errNonString)
|
||||
}
|
||||
|
||||
// Sort keys so that we have deterministic output. And write keys directly
|
||||
// underneath this key first, before writing sub-structs or sub-maps.
|
||||
var mapKeysDirect, mapKeysSub []string
|
||||
for _, mapKey := range rv.MapKeys() {
|
||||
k := mapKey.String()
|
||||
if typeIsTable(tomlTypeOfGo(eindirect(rv.MapIndex(mapKey)))) {
|
||||
mapKeysSub = append(mapKeysSub, k)
|
||||
} else {
|
||||
mapKeysDirect = append(mapKeysDirect, k)
|
||||
}
|
||||
}
|
||||
|
||||
var writeMapKeys = func(mapKeys []string, trailC bool) {
|
||||
sort.Strings(mapKeys)
|
||||
for i, mapKey := range mapKeys {
|
||||
val := eindirect(rv.MapIndex(reflect.ValueOf(mapKey)))
|
||||
if isNil(val) {
|
||||
continue
|
||||
}
|
||||
|
||||
if inline {
|
||||
enc.writeKeyValue(Key{mapKey}, val, true)
|
||||
if trailC || i != len(mapKeys)-1 {
|
||||
enc.wf(", ")
|
||||
}
|
||||
} else {
|
||||
enc.encode(key.add(mapKey), val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if inline {
|
||||
enc.wf("{")
|
||||
}
|
||||
writeMapKeys(mapKeysDirect, len(mapKeysSub) > 0)
|
||||
writeMapKeys(mapKeysSub, false)
|
||||
if inline {
|
||||
enc.wf("}")
|
||||
}
|
||||
}
|
||||
|
||||
const is32Bit = (32 << (^uint(0) >> 63)) == 32
|
||||
|
||||
func pointerTo(t reflect.Type) reflect.Type {
|
||||
if t.Kind() == reflect.Ptr {
|
||||
return pointerTo(t.Elem())
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func (enc *Encoder) eStruct(key Key, rv reflect.Value, inline bool) {
|
||||
// Write keys for fields directly under this key first, because if we write
|
||||
// a field that creates a new table then all keys under it will be in that
|
||||
// table (not the one we're writing here).
|
||||
//
|
||||
// Fields is a [][]int: for fieldsDirect this always has one entry (the
|
||||
// struct index). For fieldsSub it contains two entries: the parent field
|
||||
// index from tv, and the field indexes for the fields of the sub.
|
||||
var (
|
||||
rt = rv.Type()
|
||||
fieldsDirect, fieldsSub [][]int
|
||||
addFields func(rt reflect.Type, rv reflect.Value, start []int)
|
||||
)
|
||||
addFields = func(rt reflect.Type, rv reflect.Value, start []int) {
|
||||
for i := 0; i < rt.NumField(); i++ {
|
||||
f := rt.Field(i)
|
||||
isEmbed := f.Anonymous && pointerTo(f.Type).Kind() == reflect.Struct
|
||||
if f.PkgPath != "" && !isEmbed { /// Skip unexported fields.
|
||||
continue
|
||||
}
|
||||
opts := getOptions(f.Tag)
|
||||
if opts.skip {
|
||||
continue
|
||||
}
|
||||
|
||||
frv := eindirect(rv.Field(i))
|
||||
|
||||
// Treat anonymous struct fields with tag names as though they are
|
||||
// not anonymous, like encoding/json does.
|
||||
//
|
||||
// Non-struct anonymous fields use the normal encoding logic.
|
||||
if isEmbed {
|
||||
if getOptions(f.Tag).name == "" && frv.Kind() == reflect.Struct {
|
||||
addFields(frv.Type(), frv, append(start, f.Index...))
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if typeIsTable(tomlTypeOfGo(frv)) {
|
||||
fieldsSub = append(fieldsSub, append(start, f.Index...))
|
||||
} else {
|
||||
// Copy so it works correct on 32bit archs; not clear why this
|
||||
// is needed. See #314, and https://www.reddit.com/r/golang/comments/pnx8v4
|
||||
// This also works fine on 64bit, but 32bit archs are somewhat
|
||||
// rare and this is a wee bit faster.
|
||||
if is32Bit {
|
||||
copyStart := make([]int, len(start))
|
||||
copy(copyStart, start)
|
||||
fieldsDirect = append(fieldsDirect, append(copyStart, f.Index...))
|
||||
} else {
|
||||
fieldsDirect = append(fieldsDirect, append(start, f.Index...))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
addFields(rt, rv, nil)
|
||||
|
||||
writeFields := func(fields [][]int) {
|
||||
for _, fieldIndex := range fields {
|
||||
fieldType := rt.FieldByIndex(fieldIndex)
|
||||
fieldVal := eindirect(rv.FieldByIndex(fieldIndex))
|
||||
|
||||
if isNil(fieldVal) { /// Don't write anything for nil fields.
|
||||
continue
|
||||
}
|
||||
|
||||
opts := getOptions(fieldType.Tag)
|
||||
if opts.skip {
|
||||
continue
|
||||
}
|
||||
keyName := fieldType.Name
|
||||
if opts.name != "" {
|
||||
keyName = opts.name
|
||||
}
|
||||
|
||||
if opts.omitempty && enc.isEmpty(fieldVal) {
|
||||
continue
|
||||
}
|
||||
if opts.omitzero && isZero(fieldVal) {
|
||||
continue
|
||||
}
|
||||
|
||||
if inline {
|
||||
enc.writeKeyValue(Key{keyName}, fieldVal, true)
|
||||
if fieldIndex[0] != len(fields)-1 {
|
||||
enc.wf(", ")
|
||||
}
|
||||
} else {
|
||||
enc.encode(key.add(keyName), fieldVal)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if inline {
|
||||
enc.wf("{")
|
||||
}
|
||||
writeFields(fieldsDirect)
|
||||
writeFields(fieldsSub)
|
||||
if inline {
|
||||
enc.wf("}")
|
||||
}
|
||||
}
|
||||
|
||||
// tomlTypeOfGo returns the TOML type name of the Go value's type.
|
||||
//
|
||||
// It is used to determine whether the types of array elements are mixed (which
|
||||
// is forbidden). If the Go value is nil, then it is illegal for it to be an
|
||||
// array element, and valueIsNil is returned as true.
|
||||
//
|
||||
// The type may be `nil`, which means no concrete TOML type could be found.
|
||||
func tomlTypeOfGo(rv reflect.Value) tomlType {
|
||||
if isNil(rv) || !rv.IsValid() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if rv.Kind() == reflect.Struct {
|
||||
if rv.Type() == timeType {
|
||||
return tomlDatetime
|
||||
}
|
||||
if isMarshaler(rv) {
|
||||
return tomlString
|
||||
}
|
||||
return tomlHash
|
||||
}
|
||||
|
||||
if isMarshaler(rv) {
|
||||
return tomlString
|
||||
}
|
||||
|
||||
switch rv.Kind() {
|
||||
case reflect.Bool:
|
||||
return tomlBool
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
|
||||
reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
|
||||
reflect.Uint64:
|
||||
return tomlInteger
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return tomlFloat
|
||||
case reflect.Array, reflect.Slice:
|
||||
if isTableArray(rv) {
|
||||
return tomlArrayHash
|
||||
}
|
||||
return tomlArray
|
||||
case reflect.Ptr, reflect.Interface:
|
||||
return tomlTypeOfGo(rv.Elem())
|
||||
case reflect.String:
|
||||
return tomlString
|
||||
case reflect.Map:
|
||||
return tomlHash
|
||||
default:
|
||||
encPanic(errors.New("unsupported type: " + rv.Kind().String()))
|
||||
panic("unreachable")
|
||||
}
|
||||
}
|
||||
|
||||
func isMarshaler(rv reflect.Value) bool {
|
||||
return rv.Type().Implements(marshalText) || rv.Type().Implements(marshalToml)
|
||||
}
|
||||
|
||||
// isTableArray reports if all entries in the array or slice are a table.
|
||||
func isTableArray(arr reflect.Value) bool {
|
||||
if isNil(arr) || !arr.IsValid() || arr.Len() == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
ret := true
|
||||
for i := 0; i < arr.Len(); i++ {
|
||||
tt := tomlTypeOfGo(eindirect(arr.Index(i)))
|
||||
// Don't allow nil.
|
||||
if tt == nil {
|
||||
encPanic(errArrayNilElement)
|
||||
}
|
||||
|
||||
if ret && !typeEqual(tomlHash, tt) {
|
||||
ret = false
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
type tagOptions struct {
|
||||
skip bool // "-"
|
||||
name string
|
||||
omitempty bool
|
||||
omitzero bool
|
||||
}
|
||||
|
||||
func getOptions(tag reflect.StructTag) tagOptions {
|
||||
t := tag.Get("toml")
|
||||
if t == "-" {
|
||||
return tagOptions{skip: true}
|
||||
}
|
||||
var opts tagOptions
|
||||
parts := strings.Split(t, ",")
|
||||
opts.name = parts[0]
|
||||
for _, s := range parts[1:] {
|
||||
switch s {
|
||||
case "omitempty":
|
||||
opts.omitempty = true
|
||||
case "omitzero":
|
||||
opts.omitzero = true
|
||||
}
|
||||
}
|
||||
return opts
|
||||
}
|
||||
|
||||
func isZero(rv reflect.Value) bool {
|
||||
switch rv.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return rv.Int() == 0
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
return rv.Uint() == 0
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return rv.Float() == 0.0
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (enc *Encoder) isEmpty(rv reflect.Value) bool {
|
||||
switch rv.Kind() {
|
||||
case reflect.Array, reflect.Slice, reflect.Map, reflect.String:
|
||||
return rv.Len() == 0
|
||||
case reflect.Struct:
|
||||
if rv.Type().Comparable() {
|
||||
return reflect.Zero(rv.Type()).Interface() == rv.Interface()
|
||||
}
|
||||
// Need to also check if all the fields are empty, otherwise something
|
||||
// like this with uncomparable types will always return true:
|
||||
//
|
||||
// type a struct{ field b }
|
||||
// type b struct{ s []string }
|
||||
// s := a{field: b{s: []string{"AAA"}}}
|
||||
for i := 0; i < rv.NumField(); i++ {
|
||||
if !enc.isEmpty(rv.Field(i)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
case reflect.Bool:
|
||||
return !rv.Bool()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (enc *Encoder) newline() {
|
||||
if enc.hasWritten {
|
||||
enc.wf("\n")
|
||||
}
|
||||
}
|
||||
|
||||
// Write a key/value pair:
|
||||
//
|
||||
// key = <any value>
|
||||
//
|
||||
// This is also used for "k = v" in inline tables; so something like this will
|
||||
// be written in three calls:
|
||||
//
|
||||
// ┌───────────────────┐
|
||||
// │ ┌───┐ ┌────┐│
|
||||
// v v v v vv
|
||||
// key = {k = 1, k2 = 2}
|
||||
func (enc *Encoder) writeKeyValue(key Key, val reflect.Value, inline bool) {
|
||||
if len(key) == 0 {
|
||||
encPanic(errNoKey)
|
||||
}
|
||||
enc.wf("%s%s = ", enc.indentStr(key), key.maybeQuoted(len(key)-1))
|
||||
enc.eElement(val)
|
||||
if !inline {
|
||||
enc.newline()
|
||||
}
|
||||
}
|
||||
|
||||
func (enc *Encoder) wf(format string, v ...interface{}) {
|
||||
_, err := fmt.Fprintf(enc.w, format, v...)
|
||||
if err != nil {
|
||||
encPanic(err)
|
||||
}
|
||||
enc.hasWritten = true
|
||||
}
|
||||
|
||||
func (enc *Encoder) indentStr(key Key) string {
|
||||
return strings.Repeat(enc.Indent, len(key)-1)
|
||||
}
|
||||
|
||||
func encPanic(err error) {
|
||||
panic(tomlEncodeError{err})
|
||||
}
|
||||
|
||||
// Resolve any level of pointers to the actual value (e.g. **string → string).
|
||||
func eindirect(v reflect.Value) reflect.Value {
|
||||
if v.Kind() != reflect.Ptr && v.Kind() != reflect.Interface {
|
||||
if isMarshaler(v) {
|
||||
return v
|
||||
}
|
||||
if v.CanAddr() { /// Special case for marshalers; see #358.
|
||||
if pv := v.Addr(); isMarshaler(pv) {
|
||||
return pv
|
||||
}
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
if v.IsNil() {
|
||||
return v
|
||||
}
|
||||
|
||||
return eindirect(v.Elem())
|
||||
}
|
||||
|
||||
func isNil(rv reflect.Value) bool {
|
||||
switch rv.Kind() {
|
||||
case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
|
||||
return rv.IsNil()
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
279
vendor/github.com/BurntSushi/toml/error.go
generated
vendored
Normal file
279
vendor/github.com/BurntSushi/toml/error.go
generated
vendored
Normal file
@@ -0,0 +1,279 @@
|
||||
package toml
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ParseError is returned when there is an error parsing the TOML syntax such as
|
||||
// invalid syntax, duplicate keys, etc.
|
||||
//
|
||||
// In addition to the error message itself, you can also print detailed location
|
||||
// information with context by using [ErrorWithPosition]:
|
||||
//
|
||||
// toml: error: Key 'fruit' was already created and cannot be used as an array.
|
||||
//
|
||||
// At line 4, column 2-7:
|
||||
//
|
||||
// 2 | fruit = []
|
||||
// 3 |
|
||||
// 4 | [[fruit]] # Not allowed
|
||||
// ^^^^^
|
||||
//
|
||||
// [ErrorWithUsage] can be used to print the above with some more detailed usage
|
||||
// guidance:
|
||||
//
|
||||
// toml: error: newlines not allowed within inline tables
|
||||
//
|
||||
// At line 1, column 18:
|
||||
//
|
||||
// 1 | x = [{ key = 42 #
|
||||
// ^
|
||||
//
|
||||
// Error help:
|
||||
//
|
||||
// Inline tables must always be on a single line:
|
||||
//
|
||||
// table = {key = 42, second = 43}
|
||||
//
|
||||
// It is invalid to split them over multiple lines like so:
|
||||
//
|
||||
// # INVALID
|
||||
// table = {
|
||||
// key = 42,
|
||||
// second = 43
|
||||
// }
|
||||
//
|
||||
// Use regular for this:
|
||||
//
|
||||
// [table]
|
||||
// key = 42
|
||||
// second = 43
|
||||
type ParseError struct {
|
||||
Message string // Short technical message.
|
||||
Usage string // Longer message with usage guidance; may be blank.
|
||||
Position Position // Position of the error
|
||||
LastKey string // Last parsed key, may be blank.
|
||||
|
||||
// Line the error occurred.
|
||||
//
|
||||
// Deprecated: use [Position].
|
||||
Line int
|
||||
|
||||
err error
|
||||
input string
|
||||
}
|
||||
|
||||
// Position of an error.
|
||||
type Position struct {
|
||||
Line int // Line number, starting at 1.
|
||||
Start int // Start of error, as byte offset starting at 0.
|
||||
Len int // Lenght in bytes.
|
||||
}
|
||||
|
||||
func (pe ParseError) Error() string {
|
||||
msg := pe.Message
|
||||
if msg == "" { // Error from errorf()
|
||||
msg = pe.err.Error()
|
||||
}
|
||||
|
||||
if pe.LastKey == "" {
|
||||
return fmt.Sprintf("toml: line %d: %s", pe.Position.Line, msg)
|
||||
}
|
||||
return fmt.Sprintf("toml: line %d (last key %q): %s",
|
||||
pe.Position.Line, pe.LastKey, msg)
|
||||
}
|
||||
|
||||
// ErrorWithUsage() returns the error with detailed location context.
|
||||
//
|
||||
// See the documentation on [ParseError].
|
||||
func (pe ParseError) ErrorWithPosition() string {
|
||||
if pe.input == "" { // Should never happen, but just in case.
|
||||
return pe.Error()
|
||||
}
|
||||
|
||||
var (
|
||||
lines = strings.Split(pe.input, "\n")
|
||||
col = pe.column(lines)
|
||||
b = new(strings.Builder)
|
||||
)
|
||||
|
||||
msg := pe.Message
|
||||
if msg == "" {
|
||||
msg = pe.err.Error()
|
||||
}
|
||||
|
||||
// TODO: don't show control characters as literals? This may not show up
|
||||
// well everywhere.
|
||||
|
||||
if pe.Position.Len == 1 {
|
||||
fmt.Fprintf(b, "toml: error: %s\n\nAt line %d, column %d:\n\n",
|
||||
msg, pe.Position.Line, col+1)
|
||||
} else {
|
||||
fmt.Fprintf(b, "toml: error: %s\n\nAt line %d, column %d-%d:\n\n",
|
||||
msg, pe.Position.Line, col, col+pe.Position.Len)
|
||||
}
|
||||
if pe.Position.Line > 2 {
|
||||
fmt.Fprintf(b, "% 7d | %s\n", pe.Position.Line-2, lines[pe.Position.Line-3])
|
||||
}
|
||||
if pe.Position.Line > 1 {
|
||||
fmt.Fprintf(b, "% 7d | %s\n", pe.Position.Line-1, lines[pe.Position.Line-2])
|
||||
}
|
||||
fmt.Fprintf(b, "% 7d | %s\n", pe.Position.Line, lines[pe.Position.Line-1])
|
||||
fmt.Fprintf(b, "% 10s%s%s\n", "", strings.Repeat(" ", col), strings.Repeat("^", pe.Position.Len))
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// ErrorWithUsage() returns the error with detailed location context and usage
|
||||
// guidance.
|
||||
//
|
||||
// See the documentation on [ParseError].
|
||||
func (pe ParseError) ErrorWithUsage() string {
|
||||
m := pe.ErrorWithPosition()
|
||||
if u, ok := pe.err.(interface{ Usage() string }); ok && u.Usage() != "" {
|
||||
lines := strings.Split(strings.TrimSpace(u.Usage()), "\n")
|
||||
for i := range lines {
|
||||
if lines[i] != "" {
|
||||
lines[i] = " " + lines[i]
|
||||
}
|
||||
}
|
||||
return m + "Error help:\n\n" + strings.Join(lines, "\n") + "\n"
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func (pe ParseError) column(lines []string) int {
|
||||
var pos, col int
|
||||
for i := range lines {
|
||||
ll := len(lines[i]) + 1 // +1 for the removed newline
|
||||
if pos+ll >= pe.Position.Start {
|
||||
col = pe.Position.Start - pos
|
||||
if col < 0 { // Should never happen, but just in case.
|
||||
col = 0
|
||||
}
|
||||
break
|
||||
}
|
||||
pos += ll
|
||||
}
|
||||
|
||||
return col
|
||||
}
|
||||
|
||||
type (
|
||||
errLexControl struct{ r rune }
|
||||
errLexEscape struct{ r rune }
|
||||
errLexUTF8 struct{ b byte }
|
||||
errLexInvalidNum struct{ v string }
|
||||
errLexInvalidDate struct{ v string }
|
||||
errLexInlineTableNL struct{}
|
||||
errLexStringNL struct{}
|
||||
errParseRange struct {
|
||||
i interface{} // int or float
|
||||
size string // "int64", "uint16", etc.
|
||||
}
|
||||
errParseDuration struct{ d string }
|
||||
)
|
||||
|
||||
func (e errLexControl) Error() string {
|
||||
return fmt.Sprintf("TOML files cannot contain control characters: '0x%02x'", e.r)
|
||||
}
|
||||
func (e errLexControl) Usage() string { return "" }
|
||||
|
||||
func (e errLexEscape) Error() string { return fmt.Sprintf(`invalid escape in string '\%c'`, e.r) }
|
||||
func (e errLexEscape) Usage() string { return usageEscape }
|
||||
func (e errLexUTF8) Error() string { return fmt.Sprintf("invalid UTF-8 byte: 0x%02x", e.b) }
|
||||
func (e errLexUTF8) Usage() string { return "" }
|
||||
func (e errLexInvalidNum) Error() string { return fmt.Sprintf("invalid number: %q", e.v) }
|
||||
func (e errLexInvalidNum) Usage() string { return "" }
|
||||
func (e errLexInvalidDate) Error() string { return fmt.Sprintf("invalid date: %q", e.v) }
|
||||
func (e errLexInvalidDate) Usage() string { return "" }
|
||||
func (e errLexInlineTableNL) Error() string { return "newlines not allowed within inline tables" }
|
||||
func (e errLexInlineTableNL) Usage() string { return usageInlineNewline }
|
||||
func (e errLexStringNL) Error() string { return "strings cannot contain newlines" }
|
||||
func (e errLexStringNL) Usage() string { return usageStringNewline }
|
||||
func (e errParseRange) Error() string { return fmt.Sprintf("%v is out of range for %s", e.i, e.size) }
|
||||
func (e errParseRange) Usage() string { return usageIntOverflow }
|
||||
func (e errParseDuration) Error() string { return fmt.Sprintf("invalid duration: %q", e.d) }
|
||||
func (e errParseDuration) Usage() string { return usageDuration }
|
||||
|
||||
const usageEscape = `
|
||||
A '\' inside a "-delimited string is interpreted as an escape character.
|
||||
|
||||
The following escape sequences are supported:
|
||||
\b, \t, \n, \f, \r, \", \\, \uXXXX, and \UXXXXXXXX
|
||||
|
||||
To prevent a '\' from being recognized as an escape character, use either:
|
||||
|
||||
- a ' or '''-delimited string; escape characters aren't processed in them; or
|
||||
- write two backslashes to get a single backslash: '\\'.
|
||||
|
||||
If you're trying to add a Windows path (e.g. "C:\Users\martin") then using '/'
|
||||
instead of '\' will usually also work: "C:/Users/martin".
|
||||
`
|
||||
|
||||
const usageInlineNewline = `
|
||||
Inline tables must always be on a single line:
|
||||
|
||||
table = {key = 42, second = 43}
|
||||
|
||||
It is invalid to split them over multiple lines like so:
|
||||
|
||||
# INVALID
|
||||
table = {
|
||||
key = 42,
|
||||
second = 43
|
||||
}
|
||||
|
||||
Use regular for this:
|
||||
|
||||
[table]
|
||||
key = 42
|
||||
second = 43
|
||||
`
|
||||
|
||||
const usageStringNewline = `
|
||||
Strings must always be on a single line, and cannot span more than one line:
|
||||
|
||||
# INVALID
|
||||
string = "Hello,
|
||||
world!"
|
||||
|
||||
Instead use """ or ''' to split strings over multiple lines:
|
||||
|
||||
string = """Hello,
|
||||
world!"""
|
||||
`
|
||||
|
||||
const usageIntOverflow = `
|
||||
This number is too large; this may be an error in the TOML, but it can also be a
|
||||
bug in the program that uses too small of an integer.
|
||||
|
||||
The maximum and minimum values are:
|
||||
|
||||
size │ lowest │ highest
|
||||
───────┼────────────────┼──────────
|
||||
int8 │ -128 │ 127
|
||||
int16 │ -32,768 │ 32,767
|
||||
int32 │ -2,147,483,648 │ 2,147,483,647
|
||||
int64 │ -9.2 × 10¹⁷ │ 9.2 × 10¹⁷
|
||||
uint8 │ 0 │ 255
|
||||
uint16 │ 0 │ 65535
|
||||
uint32 │ 0 │ 4294967295
|
||||
uint64 │ 0 │ 1.8 × 10¹⁸
|
||||
|
||||
int refers to int32 on 32-bit systems and int64 on 64-bit systems.
|
||||
`
|
||||
|
||||
const usageDuration = `
|
||||
A duration must be as "number<unit>", without any spaces. Valid units are:
|
||||
|
||||
ns nanoseconds (billionth of a second)
|
||||
us, µs microseconds (millionth of a second)
|
||||
ms milliseconds (thousands of a second)
|
||||
s seconds
|
||||
m minutes
|
||||
h hours
|
||||
|
||||
You can combine multiple units; for example "5m10s" for 5 minutes and 10
|
||||
seconds.
|
||||
`
|
||||
36
vendor/github.com/BurntSushi/toml/internal/tz.go
generated
vendored
Normal file
36
vendor/github.com/BurntSushi/toml/internal/tz.go
generated
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
package internal
|
||||
|
||||
import "time"
|
||||
|
||||
// Timezones used for local datetime, date, and time TOML types.
|
||||
//
|
||||
// The exact way times and dates without a timezone should be interpreted is not
|
||||
// well-defined in the TOML specification and left to the implementation. These
|
||||
// defaults to current local timezone offset of the computer, but this can be
|
||||
// changed by changing these variables before decoding.
|
||||
//
|
||||
// TODO:
|
||||
// Ideally we'd like to offer people the ability to configure the used timezone
|
||||
// by setting Decoder.Timezone and Encoder.Timezone; however, this is a bit
|
||||
// tricky: the reason we use three different variables for this is to support
|
||||
// round-tripping – without these specific TZ names we wouldn't know which
|
||||
// format to use.
|
||||
//
|
||||
// There isn't a good way to encode this right now though, and passing this sort
|
||||
// of information also ties in to various related issues such as string format
|
||||
// encoding, encoding of comments, etc.
|
||||
//
|
||||
// So, for the time being, just put this in internal until we can write a good
|
||||
// comprehensive API for doing all of this.
|
||||
//
|
||||
// The reason they're exported is because they're referred from in e.g.
|
||||
// internal/tag.
|
||||
//
|
||||
// Note that this behaviour is valid according to the TOML spec as the exact
|
||||
// behaviour is left up to implementations.
|
||||
var (
|
||||
localOffset = func() int { _, o := time.Now().Zone(); return o }()
|
||||
LocalDatetime = time.FixedZone("datetime-local", localOffset)
|
||||
LocalDate = time.FixedZone("date-local", localOffset)
|
||||
LocalTime = time.FixedZone("time-local", localOffset)
|
||||
)
|
||||
1233
vendor/github.com/BurntSushi/toml/lex.go
generated
vendored
Normal file
1233
vendor/github.com/BurntSushi/toml/lex.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
121
vendor/github.com/BurntSushi/toml/meta.go
generated
vendored
Normal file
121
vendor/github.com/BurntSushi/toml/meta.go
generated
vendored
Normal file
@@ -0,0 +1,121 @@
|
||||
package toml
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// MetaData allows access to meta information about TOML data that's not
|
||||
// accessible otherwise.
|
||||
//
|
||||
// It allows checking if a key is defined in the TOML data, whether any keys
|
||||
// were undecoded, and the TOML type of a key.
|
||||
type MetaData struct {
|
||||
context Key // Used only during decoding.
|
||||
|
||||
keyInfo map[string]keyInfo
|
||||
mapping map[string]interface{}
|
||||
keys []Key
|
||||
decoded map[string]struct{}
|
||||
data []byte // Input file; for errors.
|
||||
}
|
||||
|
||||
// IsDefined reports if the key exists in the TOML data.
|
||||
//
|
||||
// The key should be specified hierarchically, for example to access the TOML
|
||||
// key "a.b.c" you would use IsDefined("a", "b", "c"). Keys are case sensitive.
|
||||
//
|
||||
// Returns false for an empty key.
|
||||
func (md *MetaData) IsDefined(key ...string) bool {
|
||||
if len(key) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
var (
|
||||
hash map[string]interface{}
|
||||
ok bool
|
||||
hashOrVal interface{} = md.mapping
|
||||
)
|
||||
for _, k := range key {
|
||||
if hash, ok = hashOrVal.(map[string]interface{}); !ok {
|
||||
return false
|
||||
}
|
||||
if hashOrVal, ok = hash[k]; !ok {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Type returns a string representation of the type of the key specified.
|
||||
//
|
||||
// Type will return the empty string if given an empty key or a key that does
|
||||
// not exist. Keys are case sensitive.
|
||||
func (md *MetaData) Type(key ...string) string {
|
||||
if ki, ok := md.keyInfo[Key(key).String()]; ok {
|
||||
return ki.tomlType.typeString()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Keys returns a slice of every key in the TOML data, including key groups.
|
||||
//
|
||||
// Each key is itself a slice, where the first element is the top of the
|
||||
// hierarchy and the last is the most specific. The list will have the same
|
||||
// order as the keys appeared in the TOML data.
|
||||
//
|
||||
// All keys returned are non-empty.
|
||||
func (md *MetaData) Keys() []Key {
|
||||
return md.keys
|
||||
}
|
||||
|
||||
// Undecoded returns all keys that have not been decoded in the order in which
|
||||
// they appear in the original TOML document.
|
||||
//
|
||||
// This includes keys that haven't been decoded because of a [Primitive] value.
|
||||
// Once the Primitive value is decoded, the keys will be considered decoded.
|
||||
//
|
||||
// Also note that decoding into an empty interface will result in no decoding,
|
||||
// and so no keys will be considered decoded.
|
||||
//
|
||||
// In this sense, the Undecoded keys correspond to keys in the TOML document
|
||||
// that do not have a concrete type in your representation.
|
||||
func (md *MetaData) Undecoded() []Key {
|
||||
undecoded := make([]Key, 0, len(md.keys))
|
||||
for _, key := range md.keys {
|
||||
if _, ok := md.decoded[key.String()]; !ok {
|
||||
undecoded = append(undecoded, key)
|
||||
}
|
||||
}
|
||||
return undecoded
|
||||
}
|
||||
|
||||
// Key represents any TOML key, including key groups. Use [MetaData.Keys] to get
|
||||
// values of this type.
|
||||
type Key []string
|
||||
|
||||
func (k Key) String() string {
|
||||
ss := make([]string, len(k))
|
||||
for i := range k {
|
||||
ss[i] = k.maybeQuoted(i)
|
||||
}
|
||||
return strings.Join(ss, ".")
|
||||
}
|
||||
|
||||
func (k Key) maybeQuoted(i int) string {
|
||||
if k[i] == "" {
|
||||
return `""`
|
||||
}
|
||||
for _, c := range k[i] {
|
||||
if !isBareKeyChar(c) {
|
||||
return `"` + dblQuotedReplacer.Replace(k[i]) + `"`
|
||||
}
|
||||
}
|
||||
return k[i]
|
||||
}
|
||||
|
||||
func (k Key) add(piece string) Key {
|
||||
newKey := make(Key, len(k)+1)
|
||||
copy(newKey, k)
|
||||
newKey[len(k)] = piece
|
||||
return newKey
|
||||
}
|
||||
781
vendor/github.com/BurntSushi/toml/parse.go
generated
vendored
Normal file
781
vendor/github.com/BurntSushi/toml/parse.go
generated
vendored
Normal file
@@ -0,0 +1,781 @@
|
||||
package toml
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/BurntSushi/toml/internal"
|
||||
)
|
||||
|
||||
type parser struct {
|
||||
lx *lexer
|
||||
context Key // Full key for the current hash in scope.
|
||||
currentKey string // Base key name for everything except hashes.
|
||||
pos Position // Current position in the TOML file.
|
||||
|
||||
ordered []Key // List of keys in the order that they appear in the TOML data.
|
||||
|
||||
keyInfo map[string]keyInfo // Map keyname → info about the TOML key.
|
||||
mapping map[string]interface{} // Map keyname → key value.
|
||||
implicits map[string]struct{} // Record implicit keys (e.g. "key.group.names").
|
||||
}
|
||||
|
||||
type keyInfo struct {
|
||||
pos Position
|
||||
tomlType tomlType
|
||||
}
|
||||
|
||||
func parse(data string) (p *parser, err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
if pErr, ok := r.(ParseError); ok {
|
||||
pErr.input = data
|
||||
err = pErr
|
||||
return
|
||||
}
|
||||
panic(r)
|
||||
}
|
||||
}()
|
||||
|
||||
// Read over BOM; do this here as the lexer calls utf8.DecodeRuneInString()
|
||||
// which mangles stuff.
|
||||
if strings.HasPrefix(data, "\xff\xfe") || strings.HasPrefix(data, "\xfe\xff") {
|
||||
data = data[2:]
|
||||
}
|
||||
|
||||
// Examine first few bytes for NULL bytes; this probably means it's a UTF-16
|
||||
// file (second byte in surrogate pair being NULL). Again, do this here to
|
||||
// avoid having to deal with UTF-8/16 stuff in the lexer.
|
||||
ex := 6
|
||||
if len(data) < 6 {
|
||||
ex = len(data)
|
||||
}
|
||||
if i := strings.IndexRune(data[:ex], 0); i > -1 {
|
||||
return nil, ParseError{
|
||||
Message: "files cannot contain NULL bytes; probably using UTF-16; TOML files must be UTF-8",
|
||||
Position: Position{Line: 1, Start: i, Len: 1},
|
||||
Line: 1,
|
||||
input: data,
|
||||
}
|
||||
}
|
||||
|
||||
p = &parser{
|
||||
keyInfo: make(map[string]keyInfo),
|
||||
mapping: make(map[string]interface{}),
|
||||
lx: lex(data),
|
||||
ordered: make([]Key, 0),
|
||||
implicits: make(map[string]struct{}),
|
||||
}
|
||||
for {
|
||||
item := p.next()
|
||||
if item.typ == itemEOF {
|
||||
break
|
||||
}
|
||||
p.topLevel(item)
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (p *parser) panicErr(it item, err error) {
|
||||
panic(ParseError{
|
||||
err: err,
|
||||
Position: it.pos,
|
||||
Line: it.pos.Len,
|
||||
LastKey: p.current(),
|
||||
})
|
||||
}
|
||||
|
||||
func (p *parser) panicItemf(it item, format string, v ...interface{}) {
|
||||
panic(ParseError{
|
||||
Message: fmt.Sprintf(format, v...),
|
||||
Position: it.pos,
|
||||
Line: it.pos.Len,
|
||||
LastKey: p.current(),
|
||||
})
|
||||
}
|
||||
|
||||
func (p *parser) panicf(format string, v ...interface{}) {
|
||||
panic(ParseError{
|
||||
Message: fmt.Sprintf(format, v...),
|
||||
Position: p.pos,
|
||||
Line: p.pos.Line,
|
||||
LastKey: p.current(),
|
||||
})
|
||||
}
|
||||
|
||||
func (p *parser) next() item {
|
||||
it := p.lx.nextItem()
|
||||
//fmt.Printf("ITEM %-18s line %-3d │ %q\n", it.typ, it.pos.Line, it.val)
|
||||
if it.typ == itemError {
|
||||
if it.err != nil {
|
||||
panic(ParseError{
|
||||
Position: it.pos,
|
||||
Line: it.pos.Line,
|
||||
LastKey: p.current(),
|
||||
err: it.err,
|
||||
})
|
||||
}
|
||||
|
||||
p.panicItemf(it, "%s", it.val)
|
||||
}
|
||||
return it
|
||||
}
|
||||
|
||||
func (p *parser) nextPos() item {
|
||||
it := p.next()
|
||||
p.pos = it.pos
|
||||
return it
|
||||
}
|
||||
|
||||
func (p *parser) bug(format string, v ...interface{}) {
|
||||
panic(fmt.Sprintf("BUG: "+format+"\n\n", v...))
|
||||
}
|
||||
|
||||
func (p *parser) expect(typ itemType) item {
|
||||
it := p.next()
|
||||
p.assertEqual(typ, it.typ)
|
||||
return it
|
||||
}
|
||||
|
||||
func (p *parser) assertEqual(expected, got itemType) {
|
||||
if expected != got {
|
||||
p.bug("Expected '%s' but got '%s'.", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *parser) topLevel(item item) {
|
||||
switch item.typ {
|
||||
case itemCommentStart: // # ..
|
||||
p.expect(itemText)
|
||||
case itemTableStart: // [ .. ]
|
||||
name := p.nextPos()
|
||||
|
||||
var key Key
|
||||
for ; name.typ != itemTableEnd && name.typ != itemEOF; name = p.next() {
|
||||
key = append(key, p.keyString(name))
|
||||
}
|
||||
p.assertEqual(itemTableEnd, name.typ)
|
||||
|
||||
p.addContext(key, false)
|
||||
p.setType("", tomlHash, item.pos)
|
||||
p.ordered = append(p.ordered, key)
|
||||
case itemArrayTableStart: // [[ .. ]]
|
||||
name := p.nextPos()
|
||||
|
||||
var key Key
|
||||
for ; name.typ != itemArrayTableEnd && name.typ != itemEOF; name = p.next() {
|
||||
key = append(key, p.keyString(name))
|
||||
}
|
||||
p.assertEqual(itemArrayTableEnd, name.typ)
|
||||
|
||||
p.addContext(key, true)
|
||||
p.setType("", tomlArrayHash, item.pos)
|
||||
p.ordered = append(p.ordered, key)
|
||||
case itemKeyStart: // key = ..
|
||||
outerContext := p.context
|
||||
/// Read all the key parts (e.g. 'a' and 'b' in 'a.b')
|
||||
k := p.nextPos()
|
||||
var key Key
|
||||
for ; k.typ != itemKeyEnd && k.typ != itemEOF; k = p.next() {
|
||||
key = append(key, p.keyString(k))
|
||||
}
|
||||
p.assertEqual(itemKeyEnd, k.typ)
|
||||
|
||||
/// The current key is the last part.
|
||||
p.currentKey = key[len(key)-1]
|
||||
|
||||
/// All the other parts (if any) are the context; need to set each part
|
||||
/// as implicit.
|
||||
context := key[:len(key)-1]
|
||||
for i := range context {
|
||||
p.addImplicitContext(append(p.context, context[i:i+1]...))
|
||||
}
|
||||
|
||||
/// Set value.
|
||||
vItem := p.next()
|
||||
val, typ := p.value(vItem, false)
|
||||
p.set(p.currentKey, val, typ, vItem.pos)
|
||||
p.ordered = append(p.ordered, p.context.add(p.currentKey))
|
||||
|
||||
/// Remove the context we added (preserving any context from [tbl] lines).
|
||||
p.context = outerContext
|
||||
p.currentKey = ""
|
||||
default:
|
||||
p.bug("Unexpected type at top level: %s", item.typ)
|
||||
}
|
||||
}
|
||||
|
||||
// Gets a string for a key (or part of a key in a table name).
|
||||
func (p *parser) keyString(it item) string {
|
||||
switch it.typ {
|
||||
case itemText:
|
||||
return it.val
|
||||
case itemString, itemMultilineString,
|
||||
itemRawString, itemRawMultilineString:
|
||||
s, _ := p.value(it, false)
|
||||
return s.(string)
|
||||
default:
|
||||
p.bug("Unexpected key type: %s", it.typ)
|
||||
}
|
||||
panic("unreachable")
|
||||
}
|
||||
|
||||
var datetimeRepl = strings.NewReplacer(
|
||||
"z", "Z",
|
||||
"t", "T",
|
||||
" ", "T")
|
||||
|
||||
// value translates an expected value from the lexer into a Go value wrapped
|
||||
// as an empty interface.
|
||||
func (p *parser) value(it item, parentIsArray bool) (interface{}, tomlType) {
|
||||
switch it.typ {
|
||||
case itemString:
|
||||
return p.replaceEscapes(it, it.val), p.typeOfPrimitive(it)
|
||||
case itemMultilineString:
|
||||
return p.replaceEscapes(it, stripFirstNewline(p.stripEscapedNewlines(it.val))), p.typeOfPrimitive(it)
|
||||
case itemRawString:
|
||||
return it.val, p.typeOfPrimitive(it)
|
||||
case itemRawMultilineString:
|
||||
return stripFirstNewline(it.val), p.typeOfPrimitive(it)
|
||||
case itemInteger:
|
||||
return p.valueInteger(it)
|
||||
case itemFloat:
|
||||
return p.valueFloat(it)
|
||||
case itemBool:
|
||||
switch it.val {
|
||||
case "true":
|
||||
return true, p.typeOfPrimitive(it)
|
||||
case "false":
|
||||
return false, p.typeOfPrimitive(it)
|
||||
default:
|
||||
p.bug("Expected boolean value, but got '%s'.", it.val)
|
||||
}
|
||||
case itemDatetime:
|
||||
return p.valueDatetime(it)
|
||||
case itemArray:
|
||||
return p.valueArray(it)
|
||||
case itemInlineTableStart:
|
||||
return p.valueInlineTable(it, parentIsArray)
|
||||
default:
|
||||
p.bug("Unexpected value type: %s", it.typ)
|
||||
}
|
||||
panic("unreachable")
|
||||
}
|
||||
|
||||
func (p *parser) valueInteger(it item) (interface{}, tomlType) {
|
||||
if !numUnderscoresOK(it.val) {
|
||||
p.panicItemf(it, "Invalid integer %q: underscores must be surrounded by digits", it.val)
|
||||
}
|
||||
if numHasLeadingZero(it.val) {
|
||||
p.panicItemf(it, "Invalid integer %q: cannot have leading zeroes", it.val)
|
||||
}
|
||||
|
||||
num, err := strconv.ParseInt(it.val, 0, 64)
|
||||
if err != nil {
|
||||
// Distinguish integer values. Normally, it'd be a bug if the lexer
|
||||
// provides an invalid integer, but it's possible that the number is
|
||||
// out of range of valid values (which the lexer cannot determine).
|
||||
// So mark the former as a bug but the latter as a legitimate user
|
||||
// error.
|
||||
if e, ok := err.(*strconv.NumError); ok && e.Err == strconv.ErrRange {
|
||||
p.panicErr(it, errParseRange{i: it.val, size: "int64"})
|
||||
} else {
|
||||
p.bug("Expected integer value, but got '%s'.", it.val)
|
||||
}
|
||||
}
|
||||
return num, p.typeOfPrimitive(it)
|
||||
}
|
||||
|
||||
func (p *parser) valueFloat(it item) (interface{}, tomlType) {
|
||||
parts := strings.FieldsFunc(it.val, func(r rune) bool {
|
||||
switch r {
|
||||
case '.', 'e', 'E':
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
for _, part := range parts {
|
||||
if !numUnderscoresOK(part) {
|
||||
p.panicItemf(it, "Invalid float %q: underscores must be surrounded by digits", it.val)
|
||||
}
|
||||
}
|
||||
if len(parts) > 0 && numHasLeadingZero(parts[0]) {
|
||||
p.panicItemf(it, "Invalid float %q: cannot have leading zeroes", it.val)
|
||||
}
|
||||
if !numPeriodsOK(it.val) {
|
||||
// As a special case, numbers like '123.' or '1.e2',
|
||||
// which are valid as far as Go/strconv are concerned,
|
||||
// must be rejected because TOML says that a fractional
|
||||
// part consists of '.' followed by 1+ digits.
|
||||
p.panicItemf(it, "Invalid float %q: '.' must be followed by one or more digits", it.val)
|
||||
}
|
||||
val := strings.Replace(it.val, "_", "", -1)
|
||||
if val == "+nan" || val == "-nan" { // Go doesn't support this, but TOML spec does.
|
||||
val = "nan"
|
||||
}
|
||||
num, err := strconv.ParseFloat(val, 64)
|
||||
if err != nil {
|
||||
if e, ok := err.(*strconv.NumError); ok && e.Err == strconv.ErrRange {
|
||||
p.panicErr(it, errParseRange{i: it.val, size: "float64"})
|
||||
} else {
|
||||
p.panicItemf(it, "Invalid float value: %q", it.val)
|
||||
}
|
||||
}
|
||||
return num, p.typeOfPrimitive(it)
|
||||
}
|
||||
|
||||
var dtTypes = []struct {
|
||||
fmt string
|
||||
zone *time.Location
|
||||
}{
|
||||
{time.RFC3339Nano, time.Local},
|
||||
{"2006-01-02T15:04:05.999999999", internal.LocalDatetime},
|
||||
{"2006-01-02", internal.LocalDate},
|
||||
{"15:04:05.999999999", internal.LocalTime},
|
||||
}
|
||||
|
||||
func (p *parser) valueDatetime(it item) (interface{}, tomlType) {
|
||||
it.val = datetimeRepl.Replace(it.val)
|
||||
var (
|
||||
t time.Time
|
||||
ok bool
|
||||
err error
|
||||
)
|
||||
for _, dt := range dtTypes {
|
||||
t, err = time.ParseInLocation(dt.fmt, it.val, dt.zone)
|
||||
if err == nil {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
p.panicItemf(it, "Invalid TOML Datetime: %q.", it.val)
|
||||
}
|
||||
return t, p.typeOfPrimitive(it)
|
||||
}
|
||||
|
||||
func (p *parser) valueArray(it item) (interface{}, tomlType) {
|
||||
p.setType(p.currentKey, tomlArray, it.pos)
|
||||
|
||||
var (
|
||||
types []tomlType
|
||||
|
||||
// Initialize to a non-nil empty slice. This makes it consistent with
|
||||
// how S = [] decodes into a non-nil slice inside something like struct
|
||||
// { S []string }. See #338
|
||||
array = []interface{}{}
|
||||
)
|
||||
for it = p.next(); it.typ != itemArrayEnd; it = p.next() {
|
||||
if it.typ == itemCommentStart {
|
||||
p.expect(itemText)
|
||||
continue
|
||||
}
|
||||
|
||||
val, typ := p.value(it, true)
|
||||
array = append(array, val)
|
||||
types = append(types, typ)
|
||||
|
||||
// XXX: types isn't used here, we need it to record the accurate type
|
||||
// information.
|
||||
//
|
||||
// Not entirely sure how to best store this; could use "key[0]",
|
||||
// "key[1]" notation, or maybe store it on the Array type?
|
||||
}
|
||||
return array, tomlArray
|
||||
}
|
||||
|
||||
func (p *parser) valueInlineTable(it item, parentIsArray bool) (interface{}, tomlType) {
|
||||
var (
|
||||
hash = make(map[string]interface{})
|
||||
outerContext = p.context
|
||||
outerKey = p.currentKey
|
||||
)
|
||||
|
||||
p.context = append(p.context, p.currentKey)
|
||||
prevContext := p.context
|
||||
p.currentKey = ""
|
||||
|
||||
p.addImplicit(p.context)
|
||||
p.addContext(p.context, parentIsArray)
|
||||
|
||||
/// Loop over all table key/value pairs.
|
||||
for it := p.next(); it.typ != itemInlineTableEnd; it = p.next() {
|
||||
if it.typ == itemCommentStart {
|
||||
p.expect(itemText)
|
||||
continue
|
||||
}
|
||||
|
||||
/// Read all key parts.
|
||||
k := p.nextPos()
|
||||
var key Key
|
||||
for ; k.typ != itemKeyEnd && k.typ != itemEOF; k = p.next() {
|
||||
key = append(key, p.keyString(k))
|
||||
}
|
||||
p.assertEqual(itemKeyEnd, k.typ)
|
||||
|
||||
/// The current key is the last part.
|
||||
p.currentKey = key[len(key)-1]
|
||||
|
||||
/// All the other parts (if any) are the context; need to set each part
|
||||
/// as implicit.
|
||||
context := key[:len(key)-1]
|
||||
for i := range context {
|
||||
p.addImplicitContext(append(p.context, context[i:i+1]...))
|
||||
}
|
||||
|
||||
/// Set the value.
|
||||
val, typ := p.value(p.next(), false)
|
||||
p.set(p.currentKey, val, typ, it.pos)
|
||||
p.ordered = append(p.ordered, p.context.add(p.currentKey))
|
||||
hash[p.currentKey] = val
|
||||
|
||||
/// Restore context.
|
||||
p.context = prevContext
|
||||
}
|
||||
p.context = outerContext
|
||||
p.currentKey = outerKey
|
||||
return hash, tomlHash
|
||||
}
|
||||
|
||||
// numHasLeadingZero checks if this number has leading zeroes, allowing for '0',
|
||||
// +/- signs, and base prefixes.
|
||||
func numHasLeadingZero(s string) bool {
|
||||
if len(s) > 1 && s[0] == '0' && !(s[1] == 'b' || s[1] == 'o' || s[1] == 'x') { // Allow 0b, 0o, 0x
|
||||
return true
|
||||
}
|
||||
if len(s) > 2 && (s[0] == '-' || s[0] == '+') && s[1] == '0' {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// numUnderscoresOK checks whether each underscore in s is surrounded by
|
||||
// characters that are not underscores.
|
||||
func numUnderscoresOK(s string) bool {
|
||||
switch s {
|
||||
case "nan", "+nan", "-nan", "inf", "-inf", "+inf":
|
||||
return true
|
||||
}
|
||||
accept := false
|
||||
for _, r := range s {
|
||||
if r == '_' {
|
||||
if !accept {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// isHexadecimal is a superset of all the permissable characters
|
||||
// surrounding an underscore.
|
||||
accept = isHexadecimal(r)
|
||||
}
|
||||
return accept
|
||||
}
|
||||
|
||||
// numPeriodsOK checks whether every period in s is followed by a digit.
|
||||
func numPeriodsOK(s string) bool {
|
||||
period := false
|
||||
for _, r := range s {
|
||||
if period && !isDigit(r) {
|
||||
return false
|
||||
}
|
||||
period = r == '.'
|
||||
}
|
||||
return !period
|
||||
}
|
||||
|
||||
// Set the current context of the parser, where the context is either a hash or
|
||||
// an array of hashes, depending on the value of the `array` parameter.
|
||||
//
|
||||
// Establishing the context also makes sure that the key isn't a duplicate, and
|
||||
// will create implicit hashes automatically.
|
||||
func (p *parser) addContext(key Key, array bool) {
|
||||
var ok bool
|
||||
|
||||
// Always start at the top level and drill down for our context.
|
||||
hashContext := p.mapping
|
||||
keyContext := make(Key, 0)
|
||||
|
||||
// We only need implicit hashes for key[0:-1]
|
||||
for _, k := range key[0 : len(key)-1] {
|
||||
_, ok = hashContext[k]
|
||||
keyContext = append(keyContext, k)
|
||||
|
||||
// No key? Make an implicit hash and move on.
|
||||
if !ok {
|
||||
p.addImplicit(keyContext)
|
||||
hashContext[k] = make(map[string]interface{})
|
||||
}
|
||||
|
||||
// If the hash context is actually an array of tables, then set
|
||||
// the hash context to the last element in that array.
|
||||
//
|
||||
// Otherwise, it better be a table, since this MUST be a key group (by
|
||||
// virtue of it not being the last element in a key).
|
||||
switch t := hashContext[k].(type) {
|
||||
case []map[string]interface{}:
|
||||
hashContext = t[len(t)-1]
|
||||
case map[string]interface{}:
|
||||
hashContext = t
|
||||
default:
|
||||
p.panicf("Key '%s' was already created as a hash.", keyContext)
|
||||
}
|
||||
}
|
||||
|
||||
p.context = keyContext
|
||||
if array {
|
||||
// If this is the first element for this array, then allocate a new
|
||||
// list of tables for it.
|
||||
k := key[len(key)-1]
|
||||
if _, ok := hashContext[k]; !ok {
|
||||
hashContext[k] = make([]map[string]interface{}, 0, 4)
|
||||
}
|
||||
|
||||
// Add a new table. But make sure the key hasn't already been used
|
||||
// for something else.
|
||||
if hash, ok := hashContext[k].([]map[string]interface{}); ok {
|
||||
hashContext[k] = append(hash, make(map[string]interface{}))
|
||||
} else {
|
||||
p.panicf("Key '%s' was already created and cannot be used as an array.", key)
|
||||
}
|
||||
} else {
|
||||
p.setValue(key[len(key)-1], make(map[string]interface{}))
|
||||
}
|
||||
p.context = append(p.context, key[len(key)-1])
|
||||
}
|
||||
|
||||
// set calls setValue and setType.
|
||||
func (p *parser) set(key string, val interface{}, typ tomlType, pos Position) {
|
||||
p.setValue(key, val)
|
||||
p.setType(key, typ, pos)
|
||||
|
||||
}
|
||||
|
||||
// setValue sets the given key to the given value in the current context.
|
||||
// It will make sure that the key hasn't already been defined, account for
|
||||
// implicit key groups.
|
||||
func (p *parser) setValue(key string, value interface{}) {
|
||||
var (
|
||||
tmpHash interface{}
|
||||
ok bool
|
||||
hash = p.mapping
|
||||
keyContext Key
|
||||
)
|
||||
for _, k := range p.context {
|
||||
keyContext = append(keyContext, k)
|
||||
if tmpHash, ok = hash[k]; !ok {
|
||||
p.bug("Context for key '%s' has not been established.", keyContext)
|
||||
}
|
||||
switch t := tmpHash.(type) {
|
||||
case []map[string]interface{}:
|
||||
// The context is a table of hashes. Pick the most recent table
|
||||
// defined as the current hash.
|
||||
hash = t[len(t)-1]
|
||||
case map[string]interface{}:
|
||||
hash = t
|
||||
default:
|
||||
p.panicf("Key '%s' has already been defined.", keyContext)
|
||||
}
|
||||
}
|
||||
keyContext = append(keyContext, key)
|
||||
|
||||
if _, ok := hash[key]; ok {
|
||||
// Normally redefining keys isn't allowed, but the key could have been
|
||||
// defined implicitly and it's allowed to be redefined concretely. (See
|
||||
// the `valid/implicit-and-explicit-after.toml` in toml-test)
|
||||
//
|
||||
// But we have to make sure to stop marking it as an implicit. (So that
|
||||
// another redefinition provokes an error.)
|
||||
//
|
||||
// Note that since it has already been defined (as a hash), we don't
|
||||
// want to overwrite it. So our business is done.
|
||||
if p.isArray(keyContext) {
|
||||
p.removeImplicit(keyContext)
|
||||
hash[key] = value
|
||||
return
|
||||
}
|
||||
if p.isImplicit(keyContext) {
|
||||
p.removeImplicit(keyContext)
|
||||
return
|
||||
}
|
||||
|
||||
// Otherwise, we have a concrete key trying to override a previous
|
||||
// key, which is *always* wrong.
|
||||
p.panicf("Key '%s' has already been defined.", keyContext)
|
||||
}
|
||||
|
||||
hash[key] = value
|
||||
}
|
||||
|
||||
// setType sets the type of a particular value at a given key. It should be
|
||||
// called immediately AFTER setValue.
|
||||
//
|
||||
// Note that if `key` is empty, then the type given will be applied to the
|
||||
// current context (which is either a table or an array of tables).
|
||||
func (p *parser) setType(key string, typ tomlType, pos Position) {
|
||||
keyContext := make(Key, 0, len(p.context)+1)
|
||||
keyContext = append(keyContext, p.context...)
|
||||
if len(key) > 0 { // allow type setting for hashes
|
||||
keyContext = append(keyContext, key)
|
||||
}
|
||||
// Special case to make empty keys ("" = 1) work.
|
||||
// Without it it will set "" rather than `""`.
|
||||
// TODO: why is this needed? And why is this only needed here?
|
||||
if len(keyContext) == 0 {
|
||||
keyContext = Key{""}
|
||||
}
|
||||
p.keyInfo[keyContext.String()] = keyInfo{tomlType: typ, pos: pos}
|
||||
}
|
||||
|
||||
// Implicit keys need to be created when tables are implied in "a.b.c.d = 1" and
|
||||
// "[a.b.c]" (the "a", "b", and "c" hashes are never created explicitly).
|
||||
func (p *parser) addImplicit(key Key) { p.implicits[key.String()] = struct{}{} }
|
||||
func (p *parser) removeImplicit(key Key) { delete(p.implicits, key.String()) }
|
||||
func (p *parser) isImplicit(key Key) bool { _, ok := p.implicits[key.String()]; return ok }
|
||||
func (p *parser) isArray(key Key) bool { return p.keyInfo[key.String()].tomlType == tomlArray }
|
||||
func (p *parser) addImplicitContext(key Key) {
|
||||
p.addImplicit(key)
|
||||
p.addContext(key, false)
|
||||
}
|
||||
|
||||
// current returns the full key name of the current context.
|
||||
func (p *parser) current() string {
|
||||
if len(p.currentKey) == 0 {
|
||||
return p.context.String()
|
||||
}
|
||||
if len(p.context) == 0 {
|
||||
return p.currentKey
|
||||
}
|
||||
return fmt.Sprintf("%s.%s", p.context, p.currentKey)
|
||||
}
|
||||
|
||||
func stripFirstNewline(s string) string {
|
||||
if len(s) > 0 && s[0] == '\n' {
|
||||
return s[1:]
|
||||
}
|
||||
if len(s) > 1 && s[0] == '\r' && s[1] == '\n' {
|
||||
return s[2:]
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Remove newlines inside triple-quoted strings if a line ends with "\".
|
||||
func (p *parser) stripEscapedNewlines(s string) string {
|
||||
split := strings.Split(s, "\n")
|
||||
if len(split) < 1 {
|
||||
return s
|
||||
}
|
||||
|
||||
escNL := false // Keep track of the last non-blank line was escaped.
|
||||
for i, line := range split {
|
||||
line = strings.TrimRight(line, " \t\r")
|
||||
|
||||
if len(line) == 0 || line[len(line)-1] != '\\' {
|
||||
split[i] = strings.TrimRight(split[i], "\r")
|
||||
if !escNL && i != len(split)-1 {
|
||||
split[i] += "\n"
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
escBS := true
|
||||
for j := len(line) - 1; j >= 0 && line[j] == '\\'; j-- {
|
||||
escBS = !escBS
|
||||
}
|
||||
if escNL {
|
||||
line = strings.TrimLeft(line, " \t\r")
|
||||
}
|
||||
escNL = !escBS
|
||||
|
||||
if escBS {
|
||||
split[i] += "\n"
|
||||
continue
|
||||
}
|
||||
|
||||
if i == len(split)-1 {
|
||||
p.panicf("invalid escape: '\\ '")
|
||||
}
|
||||
|
||||
split[i] = line[:len(line)-1] // Remove \
|
||||
if len(split)-1 > i {
|
||||
split[i+1] = strings.TrimLeft(split[i+1], " \t\r")
|
||||
}
|
||||
}
|
||||
return strings.Join(split, "")
|
||||
}
|
||||
|
||||
func (p *parser) replaceEscapes(it item, str string) string {
|
||||
replaced := make([]rune, 0, len(str))
|
||||
s := []byte(str)
|
||||
r := 0
|
||||
for r < len(s) {
|
||||
if s[r] != '\\' {
|
||||
c, size := utf8.DecodeRune(s[r:])
|
||||
r += size
|
||||
replaced = append(replaced, c)
|
||||
continue
|
||||
}
|
||||
r += 1
|
||||
if r >= len(s) {
|
||||
p.bug("Escape sequence at end of string.")
|
||||
return ""
|
||||
}
|
||||
switch s[r] {
|
||||
default:
|
||||
p.bug("Expected valid escape code after \\, but got %q.", s[r])
|
||||
case ' ', '\t':
|
||||
p.panicItemf(it, "invalid escape: '\\%c'", s[r])
|
||||
case 'b':
|
||||
replaced = append(replaced, rune(0x0008))
|
||||
r += 1
|
||||
case 't':
|
||||
replaced = append(replaced, rune(0x0009))
|
||||
r += 1
|
||||
case 'n':
|
||||
replaced = append(replaced, rune(0x000A))
|
||||
r += 1
|
||||
case 'f':
|
||||
replaced = append(replaced, rune(0x000C))
|
||||
r += 1
|
||||
case 'r':
|
||||
replaced = append(replaced, rune(0x000D))
|
||||
r += 1
|
||||
case '"':
|
||||
replaced = append(replaced, rune(0x0022))
|
||||
r += 1
|
||||
case '\\':
|
||||
replaced = append(replaced, rune(0x005C))
|
||||
r += 1
|
||||
case 'u':
|
||||
// At this point, we know we have a Unicode escape of the form
|
||||
// `uXXXX` at [r, r+5). (Because the lexer guarantees this
|
||||
// for us.)
|
||||
escaped := p.asciiEscapeToUnicode(it, s[r+1:r+5])
|
||||
replaced = append(replaced, escaped)
|
||||
r += 5
|
||||
case 'U':
|
||||
// At this point, we know we have a Unicode escape of the form
|
||||
// `uXXXX` at [r, r+9). (Because the lexer guarantees this
|
||||
// for us.)
|
||||
escaped := p.asciiEscapeToUnicode(it, s[r+1:r+9])
|
||||
replaced = append(replaced, escaped)
|
||||
r += 9
|
||||
}
|
||||
}
|
||||
return string(replaced)
|
||||
}
|
||||
|
||||
func (p *parser) asciiEscapeToUnicode(it item, bs []byte) rune {
|
||||
s := string(bs)
|
||||
hex, err := strconv.ParseUint(strings.ToLower(s), 16, 32)
|
||||
if err != nil {
|
||||
p.bug("Could not parse '%s' as a hexadecimal number, but the lexer claims it's OK: %s", s, err)
|
||||
}
|
||||
if !utf8.ValidRune(rune(hex)) {
|
||||
p.panicItemf(it, "Escaped character '\\u%s' is not valid UTF-8.", s)
|
||||
}
|
||||
return rune(hex)
|
||||
}
|
||||
242
vendor/github.com/BurntSushi/toml/type_fields.go
generated
vendored
Normal file
242
vendor/github.com/BurntSushi/toml/type_fields.go
generated
vendored
Normal file
@@ -0,0 +1,242 @@
|
||||
package toml
|
||||
|
||||
// Struct field handling is adapted from code in encoding/json:
|
||||
//
|
||||
// Copyright 2010 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the Go distribution.
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sort"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// A field represents a single field found in a struct.
|
||||
type field struct {
|
||||
name string // the name of the field (`toml` tag included)
|
||||
tag bool // whether field has a `toml` tag
|
||||
index []int // represents the depth of an anonymous field
|
||||
typ reflect.Type // the type of the field
|
||||
}
|
||||
|
||||
// byName sorts field by name, breaking ties with depth,
|
||||
// then breaking ties with "name came from toml tag", then
|
||||
// breaking ties with index sequence.
|
||||
type byName []field
|
||||
|
||||
func (x byName) Len() int { return len(x) }
|
||||
|
||||
func (x byName) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
||||
|
||||
func (x byName) Less(i, j int) bool {
|
||||
if x[i].name != x[j].name {
|
||||
return x[i].name < x[j].name
|
||||
}
|
||||
if len(x[i].index) != len(x[j].index) {
|
||||
return len(x[i].index) < len(x[j].index)
|
||||
}
|
||||
if x[i].tag != x[j].tag {
|
||||
return x[i].tag
|
||||
}
|
||||
return byIndex(x).Less(i, j)
|
||||
}
|
||||
|
||||
// byIndex sorts field by index sequence.
|
||||
type byIndex []field
|
||||
|
||||
func (x byIndex) Len() int { return len(x) }
|
||||
|
||||
func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
||||
|
||||
func (x byIndex) Less(i, j int) bool {
|
||||
for k, xik := range x[i].index {
|
||||
if k >= len(x[j].index) {
|
||||
return false
|
||||
}
|
||||
if xik != x[j].index[k] {
|
||||
return xik < x[j].index[k]
|
||||
}
|
||||
}
|
||||
return len(x[i].index) < len(x[j].index)
|
||||
}
|
||||
|
||||
// typeFields returns a list of fields that TOML should recognize for the given
|
||||
// type. The algorithm is breadth-first search over the set of structs to
|
||||
// include - the top struct and then any reachable anonymous structs.
|
||||
func typeFields(t reflect.Type) []field {
|
||||
// Anonymous fields to explore at the current level and the next.
|
||||
current := []field{}
|
||||
next := []field{{typ: t}}
|
||||
|
||||
// Count of queued names for current level and the next.
|
||||
var count map[reflect.Type]int
|
||||
var nextCount map[reflect.Type]int
|
||||
|
||||
// Types already visited at an earlier level.
|
||||
visited := map[reflect.Type]bool{}
|
||||
|
||||
// Fields found.
|
||||
var fields []field
|
||||
|
||||
for len(next) > 0 {
|
||||
current, next = next, current[:0]
|
||||
count, nextCount = nextCount, map[reflect.Type]int{}
|
||||
|
||||
for _, f := range current {
|
||||
if visited[f.typ] {
|
||||
continue
|
||||
}
|
||||
visited[f.typ] = true
|
||||
|
||||
// Scan f.typ for fields to include.
|
||||
for i := 0; i < f.typ.NumField(); i++ {
|
||||
sf := f.typ.Field(i)
|
||||
if sf.PkgPath != "" && !sf.Anonymous { // unexported
|
||||
continue
|
||||
}
|
||||
opts := getOptions(sf.Tag)
|
||||
if opts.skip {
|
||||
continue
|
||||
}
|
||||
index := make([]int, len(f.index)+1)
|
||||
copy(index, f.index)
|
||||
index[len(f.index)] = i
|
||||
|
||||
ft := sf.Type
|
||||
if ft.Name() == "" && ft.Kind() == reflect.Ptr {
|
||||
// Follow pointer.
|
||||
ft = ft.Elem()
|
||||
}
|
||||
|
||||
// Record found field and index sequence.
|
||||
if opts.name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct {
|
||||
tagged := opts.name != ""
|
||||
name := opts.name
|
||||
if name == "" {
|
||||
name = sf.Name
|
||||
}
|
||||
fields = append(fields, field{name, tagged, index, ft})
|
||||
if count[f.typ] > 1 {
|
||||
// If there were multiple instances, add a second,
|
||||
// so that the annihilation code will see a duplicate.
|
||||
// It only cares about the distinction between 1 or 2,
|
||||
// so don't bother generating any more copies.
|
||||
fields = append(fields, fields[len(fields)-1])
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Record new anonymous struct to explore in next round.
|
||||
nextCount[ft]++
|
||||
if nextCount[ft] == 1 {
|
||||
f := field{name: ft.Name(), index: index, typ: ft}
|
||||
next = append(next, f)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sort.Sort(byName(fields))
|
||||
|
||||
// Delete all fields that are hidden by the Go rules for embedded fields,
|
||||
// except that fields with TOML tags are promoted.
|
||||
|
||||
// The fields are sorted in primary order of name, secondary order
|
||||
// of field index length. Loop over names; for each name, delete
|
||||
// hidden fields by choosing the one dominant field that survives.
|
||||
out := fields[:0]
|
||||
for advance, i := 0, 0; i < len(fields); i += advance {
|
||||
// One iteration per name.
|
||||
// Find the sequence of fields with the name of this first field.
|
||||
fi := fields[i]
|
||||
name := fi.name
|
||||
for advance = 1; i+advance < len(fields); advance++ {
|
||||
fj := fields[i+advance]
|
||||
if fj.name != name {
|
||||
break
|
||||
}
|
||||
}
|
||||
if advance == 1 { // Only one field with this name
|
||||
out = append(out, fi)
|
||||
continue
|
||||
}
|
||||
dominant, ok := dominantField(fields[i : i+advance])
|
||||
if ok {
|
||||
out = append(out, dominant)
|
||||
}
|
||||
}
|
||||
|
||||
fields = out
|
||||
sort.Sort(byIndex(fields))
|
||||
|
||||
return fields
|
||||
}
|
||||
|
||||
// dominantField looks through the fields, all of which are known to
|
||||
// have the same name, to find the single field that dominates the
|
||||
// others using Go's embedding rules, modified by the presence of
|
||||
// TOML tags. If there are multiple top-level fields, the boolean
|
||||
// will be false: This condition is an error in Go and we skip all
|
||||
// the fields.
|
||||
func dominantField(fields []field) (field, bool) {
|
||||
// The fields are sorted in increasing index-length order. The winner
|
||||
// must therefore be one with the shortest index length. Drop all
|
||||
// longer entries, which is easy: just truncate the slice.
|
||||
length := len(fields[0].index)
|
||||
tagged := -1 // Index of first tagged field.
|
||||
for i, f := range fields {
|
||||
if len(f.index) > length {
|
||||
fields = fields[:i]
|
||||
break
|
||||
}
|
||||
if f.tag {
|
||||
if tagged >= 0 {
|
||||
// Multiple tagged fields at the same level: conflict.
|
||||
// Return no field.
|
||||
return field{}, false
|
||||
}
|
||||
tagged = i
|
||||
}
|
||||
}
|
||||
if tagged >= 0 {
|
||||
return fields[tagged], true
|
||||
}
|
||||
// All remaining fields have the same length. If there's more than one,
|
||||
// we have a conflict (two fields named "X" at the same level) and we
|
||||
// return no field.
|
||||
if len(fields) > 1 {
|
||||
return field{}, false
|
||||
}
|
||||
return fields[0], true
|
||||
}
|
||||
|
||||
var fieldCache struct {
|
||||
sync.RWMutex
|
||||
m map[reflect.Type][]field
|
||||
}
|
||||
|
||||
// cachedTypeFields is like typeFields but uses a cache to avoid repeated work.
|
||||
func cachedTypeFields(t reflect.Type) []field {
|
||||
fieldCache.RLock()
|
||||
f := fieldCache.m[t]
|
||||
fieldCache.RUnlock()
|
||||
if f != nil {
|
||||
return f
|
||||
}
|
||||
|
||||
// Compute fields without lock.
|
||||
// Might duplicate effort but won't hold other computations back.
|
||||
f = typeFields(t)
|
||||
if f == nil {
|
||||
f = []field{}
|
||||
}
|
||||
|
||||
fieldCache.Lock()
|
||||
if fieldCache.m == nil {
|
||||
fieldCache.m = map[reflect.Type][]field{}
|
||||
}
|
||||
fieldCache.m[t] = f
|
||||
fieldCache.Unlock()
|
||||
return f
|
||||
}
|
||||
70
vendor/github.com/BurntSushi/toml/type_toml.go
generated
vendored
Normal file
70
vendor/github.com/BurntSushi/toml/type_toml.go
generated
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
package toml
|
||||
|
||||
// tomlType represents any Go type that corresponds to a TOML type.
|
||||
// While the first draft of the TOML spec has a simplistic type system that
|
||||
// probably doesn't need this level of sophistication, we seem to be militating
|
||||
// toward adding real composite types.
|
||||
type tomlType interface {
|
||||
typeString() string
|
||||
}
|
||||
|
||||
// typeEqual accepts any two types and returns true if they are equal.
|
||||
func typeEqual(t1, t2 tomlType) bool {
|
||||
if t1 == nil || t2 == nil {
|
||||
return false
|
||||
}
|
||||
return t1.typeString() == t2.typeString()
|
||||
}
|
||||
|
||||
func typeIsTable(t tomlType) bool {
|
||||
return typeEqual(t, tomlHash) || typeEqual(t, tomlArrayHash)
|
||||
}
|
||||
|
||||
type tomlBaseType string
|
||||
|
||||
func (btype tomlBaseType) typeString() string {
|
||||
return string(btype)
|
||||
}
|
||||
|
||||
func (btype tomlBaseType) String() string {
|
||||
return btype.typeString()
|
||||
}
|
||||
|
||||
var (
|
||||
tomlInteger tomlBaseType = "Integer"
|
||||
tomlFloat tomlBaseType = "Float"
|
||||
tomlDatetime tomlBaseType = "Datetime"
|
||||
tomlString tomlBaseType = "String"
|
||||
tomlBool tomlBaseType = "Bool"
|
||||
tomlArray tomlBaseType = "Array"
|
||||
tomlHash tomlBaseType = "Hash"
|
||||
tomlArrayHash tomlBaseType = "ArrayHash"
|
||||
)
|
||||
|
||||
// typeOfPrimitive returns a tomlType of any primitive value in TOML.
|
||||
// Primitive values are: Integer, Float, Datetime, String and Bool.
|
||||
//
|
||||
// Passing a lexer item other than the following will cause a BUG message
|
||||
// to occur: itemString, itemBool, itemInteger, itemFloat, itemDatetime.
|
||||
func (p *parser) typeOfPrimitive(lexItem item) tomlType {
|
||||
switch lexItem.typ {
|
||||
case itemInteger:
|
||||
return tomlInteger
|
||||
case itemFloat:
|
||||
return tomlFloat
|
||||
case itemDatetime:
|
||||
return tomlDatetime
|
||||
case itemString:
|
||||
return tomlString
|
||||
case itemMultilineString:
|
||||
return tomlString
|
||||
case itemRawString:
|
||||
return tomlString
|
||||
case itemRawMultilineString:
|
||||
return tomlString
|
||||
case itemBool:
|
||||
return tomlBool
|
||||
}
|
||||
p.bug("Cannot infer primitive type of lex item '%s'.", lexItem)
|
||||
panic("unreachable")
|
||||
}
|
||||
18
vendor/github.com/Masterminds/goutils/.travis.yml
generated
vendored
Normal file
18
vendor/github.com/Masterminds/goutils/.travis.yml
generated
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.6
|
||||
- 1.7
|
||||
- 1.8
|
||||
- tip
|
||||
|
||||
script:
|
||||
- go test -v
|
||||
|
||||
notifications:
|
||||
webhooks:
|
||||
urls:
|
||||
- https://webhooks.gitter.im/e/06e3328629952dabe3e0
|
||||
on_success: change # options: [always|never|change] default: always
|
||||
on_failure: always # options: [always|never|change] default: always
|
||||
on_start: never # options: [always|never|change] default: always
|
||||
8
vendor/github.com/Masterminds/goutils/CHANGELOG.md
generated
vendored
Normal file
8
vendor/github.com/Masterminds/goutils/CHANGELOG.md
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
# 1.0.1 (2017-05-31)
|
||||
|
||||
## Fixed
|
||||
- #21: Fix generation of alphanumeric strings (thanks @dbarranco)
|
||||
|
||||
# 1.0.0 (2014-04-30)
|
||||
|
||||
- Initial release.
|
||||
202
vendor/github.com/Masterminds/goutils/LICENSE.txt
generated
vendored
Normal file
202
vendor/github.com/Masterminds/goutils/LICENSE.txt
generated
vendored
Normal file
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
70
vendor/github.com/Masterminds/goutils/README.md
generated
vendored
Normal file
70
vendor/github.com/Masterminds/goutils/README.md
generated
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
GoUtils
|
||||
===========
|
||||
[](https://masterminds.github.io/stability/maintenance.html)
|
||||
[](https://godoc.org/github.com/Masterminds/goutils) [](https://travis-ci.org/Masterminds/goutils) [](https://ci.appveyor.com/project/mattfarina/goutils)
|
||||
|
||||
|
||||
GoUtils provides users with utility functions to manipulate strings in various ways. It is a Go implementation of some
|
||||
string manipulation libraries of Java Apache Commons. GoUtils includes the following Java Apache Commons classes:
|
||||
* WordUtils
|
||||
* RandomStringUtils
|
||||
* StringUtils (partial implementation)
|
||||
|
||||
## Installation
|
||||
If you have Go set up on your system, from the GOPATH directory within the command line/terminal, enter this:
|
||||
|
||||
go get github.com/Masterminds/goutils
|
||||
|
||||
If you do not have Go set up on your system, please follow the [Go installation directions from the documenation](http://golang.org/doc/install), and then follow the instructions above to install GoUtils.
|
||||
|
||||
|
||||
## Documentation
|
||||
GoUtils doc is available here: [](https://godoc.org/github.com/Masterminds/goutils)
|
||||
|
||||
|
||||
## Usage
|
||||
The code snippets below show examples of how to use GoUtils. Some functions return errors while others do not. The first instance below, which does not return an error, is the `Initials` function (located within the `wordutils.go` file).
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Masterminds/goutils"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
// EXAMPLE 1: A goutils function which returns no errors
|
||||
fmt.Println (goutils.Initials("John Doe Foo")) // Prints out "JDF"
|
||||
|
||||
}
|
||||
Some functions return errors mainly due to illegal arguements used as parameters. The code example below illustrates how to deal with function that returns an error. In this instance, the function is the `Random` function (located within the `randomstringutils.go` file).
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Masterminds/goutils"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
// EXAMPLE 2: A goutils function which returns an error
|
||||
rand1, err1 := goutils.Random (-1, 0, 0, true, true)
|
||||
|
||||
if err1 != nil {
|
||||
fmt.Println(err1) // Prints out error message because -1 was entered as the first parameter in goutils.Random(...)
|
||||
} else {
|
||||
fmt.Println(rand1)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
## License
|
||||
GoUtils is licensed under the Apache License, Version 2.0. Please check the LICENSE.txt file or visit http://www.apache.org/licenses/LICENSE-2.0 for a copy of the license.
|
||||
|
||||
## Issue Reporting
|
||||
Make suggestions or report issues using the Git issue tracker: https://github.com/Masterminds/goutils/issues
|
||||
|
||||
## Website
|
||||
* [GoUtils webpage](http://Masterminds.github.io/goutils/)
|
||||
21
vendor/github.com/Masterminds/goutils/appveyor.yml
generated
vendored
Normal file
21
vendor/github.com/Masterminds/goutils/appveyor.yml
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
version: build-{build}.{branch}
|
||||
|
||||
clone_folder: C:\gopath\src\github.com\Masterminds\goutils
|
||||
shallow_clone: true
|
||||
|
||||
environment:
|
||||
GOPATH: C:\gopath
|
||||
|
||||
platform:
|
||||
- x64
|
||||
|
||||
build: off
|
||||
|
||||
install:
|
||||
- go version
|
||||
- go env
|
||||
|
||||
test_script:
|
||||
- go test -v
|
||||
|
||||
deploy: off
|
||||
230
vendor/github.com/Masterminds/goutils/cryptorandomstringutils.go
generated
vendored
Normal file
230
vendor/github.com/Masterminds/goutils/cryptorandomstringutils.go
generated
vendored
Normal file
@@ -0,0 +1,230 @@
|
||||
/*
|
||||
Copyright 2014 Alexander Okoli
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package goutils
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/big"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
/*
|
||||
CryptoRandomNonAlphaNumeric creates a random string whose length is the number of characters specified.
|
||||
Characters will be chosen from the set of all characters (ASCII/Unicode values between 0 to 2,147,483,647 (math.MaxInt32)).
|
||||
|
||||
Parameter:
|
||||
count - the length of random string to create
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from an invalid parameter within underlying function, CryptoRandom(...)
|
||||
*/
|
||||
func CryptoRandomNonAlphaNumeric(count int) (string, error) {
|
||||
return CryptoRandomAlphaNumericCustom(count, false, false)
|
||||
}
|
||||
|
||||
/*
|
||||
CryptoRandomAscii creates a random string whose length is the number of characters specified.
|
||||
Characters will be chosen from the set of characters whose ASCII value is between 32 and 126 (inclusive).
|
||||
|
||||
Parameter:
|
||||
count - the length of random string to create
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from an invalid parameter within underlying function, CryptoRandom(...)
|
||||
*/
|
||||
func CryptoRandomAscii(count int) (string, error) {
|
||||
return CryptoRandom(count, 32, 127, false, false)
|
||||
}
|
||||
|
||||
/*
|
||||
CryptoRandomNumeric creates a random string whose length is the number of characters specified.
|
||||
Characters will be chosen from the set of numeric characters.
|
||||
|
||||
Parameter:
|
||||
count - the length of random string to create
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from an invalid parameter within underlying function, CryptoRandom(...)
|
||||
*/
|
||||
func CryptoRandomNumeric(count int) (string, error) {
|
||||
return CryptoRandom(count, 0, 0, false, true)
|
||||
}
|
||||
|
||||
/*
|
||||
CryptoRandomAlphabetic creates a random string whose length is the number of characters specified.
|
||||
Characters will be chosen from the set of alpha-numeric characters as indicated by the arguments.
|
||||
|
||||
Parameters:
|
||||
count - the length of random string to create
|
||||
letters - if true, generated string may include alphabetic characters
|
||||
numbers - if true, generated string may include numeric characters
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from an invalid parameter within underlying function, CryptoRandom(...)
|
||||
*/
|
||||
func CryptoRandomAlphabetic(count int) (string, error) {
|
||||
return CryptoRandom(count, 0, 0, true, false)
|
||||
}
|
||||
|
||||
/*
|
||||
CryptoRandomAlphaNumeric creates a random string whose length is the number of characters specified.
|
||||
Characters will be chosen from the set of alpha-numeric characters.
|
||||
|
||||
Parameter:
|
||||
count - the length of random string to create
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from an invalid parameter within underlying function, CryptoRandom(...)
|
||||
*/
|
||||
func CryptoRandomAlphaNumeric(count int) (string, error) {
|
||||
return CryptoRandom(count, 0, 0, true, true)
|
||||
}
|
||||
|
||||
/*
|
||||
CryptoRandomAlphaNumericCustom creates a random string whose length is the number of characters specified.
|
||||
Characters will be chosen from the set of alpha-numeric characters as indicated by the arguments.
|
||||
|
||||
Parameters:
|
||||
count - the length of random string to create
|
||||
letters - if true, generated string may include alphabetic characters
|
||||
numbers - if true, generated string may include numeric characters
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from an invalid parameter within underlying function, CryptoRandom(...)
|
||||
*/
|
||||
func CryptoRandomAlphaNumericCustom(count int, letters bool, numbers bool) (string, error) {
|
||||
return CryptoRandom(count, 0, 0, letters, numbers)
|
||||
}
|
||||
|
||||
/*
|
||||
CryptoRandom creates a random string based on a variety of options, using using golang's crypto/rand source of randomness.
|
||||
If the parameters start and end are both 0, start and end are set to ' ' and 'z', the ASCII printable characters, will be used,
|
||||
unless letters and numbers are both false, in which case, start and end are set to 0 and math.MaxInt32, respectively.
|
||||
If chars is not nil, characters stored in chars that are between start and end are chosen.
|
||||
|
||||
Parameters:
|
||||
count - the length of random string to create
|
||||
start - the position in set of chars (ASCII/Unicode int) to start at
|
||||
end - the position in set of chars (ASCII/Unicode int) to end before
|
||||
letters - if true, generated string may include alphabetic characters
|
||||
numbers - if true, generated string may include numeric characters
|
||||
chars - the set of chars to choose randoms from. If nil, then it will use the set of all chars.
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from invalid parameters: if count < 0; or the provided chars array is empty; or end <= start; or end > len(chars)
|
||||
*/
|
||||
func CryptoRandom(count int, start int, end int, letters bool, numbers bool, chars ...rune) (string, error) {
|
||||
if count == 0 {
|
||||
return "", nil
|
||||
} else if count < 0 {
|
||||
err := fmt.Errorf("randomstringutils illegal argument: Requested random string length %v is less than 0.", count) // equiv to err := errors.New("...")
|
||||
return "", err
|
||||
}
|
||||
if chars != nil && len(chars) == 0 {
|
||||
err := fmt.Errorf("randomstringutils illegal argument: The chars array must not be empty")
|
||||
return "", err
|
||||
}
|
||||
|
||||
if start == 0 && end == 0 {
|
||||
if chars != nil {
|
||||
end = len(chars)
|
||||
} else {
|
||||
if !letters && !numbers {
|
||||
end = math.MaxInt32
|
||||
} else {
|
||||
end = 'z' + 1
|
||||
start = ' '
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if end <= start {
|
||||
err := fmt.Errorf("randomstringutils illegal argument: Parameter end (%v) must be greater than start (%v)", end, start)
|
||||
return "", err
|
||||
}
|
||||
|
||||
if chars != nil && end > len(chars) {
|
||||
err := fmt.Errorf("randomstringutils illegal argument: Parameter end (%v) cannot be greater than len(chars) (%v)", end, len(chars))
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
buffer := make([]rune, count)
|
||||
gap := end - start
|
||||
|
||||
// high-surrogates range, (\uD800-\uDBFF) = 55296 - 56319
|
||||
// low-surrogates range, (\uDC00-\uDFFF) = 56320 - 57343
|
||||
|
||||
for count != 0 {
|
||||
count--
|
||||
var ch rune
|
||||
if chars == nil {
|
||||
ch = rune(getCryptoRandomInt(gap) + int64(start))
|
||||
} else {
|
||||
ch = chars[getCryptoRandomInt(gap)+int64(start)]
|
||||
}
|
||||
|
||||
if letters && unicode.IsLetter(ch) || numbers && unicode.IsDigit(ch) || !letters && !numbers {
|
||||
if ch >= 56320 && ch <= 57343 { // low surrogate range
|
||||
if count == 0 {
|
||||
count++
|
||||
} else {
|
||||
// Insert low surrogate
|
||||
buffer[count] = ch
|
||||
count--
|
||||
// Insert high surrogate
|
||||
buffer[count] = rune(55296 + getCryptoRandomInt(128))
|
||||
}
|
||||
} else if ch >= 55296 && ch <= 56191 { // High surrogates range (Partial)
|
||||
if count == 0 {
|
||||
count++
|
||||
} else {
|
||||
// Insert low surrogate
|
||||
buffer[count] = rune(56320 + getCryptoRandomInt(128))
|
||||
count--
|
||||
// Insert high surrogate
|
||||
buffer[count] = ch
|
||||
}
|
||||
} else if ch >= 56192 && ch <= 56319 {
|
||||
// private high surrogate, skip it
|
||||
count++
|
||||
} else {
|
||||
// not one of the surrogates*
|
||||
buffer[count] = ch
|
||||
}
|
||||
} else {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return string(buffer), nil
|
||||
}
|
||||
|
||||
func getCryptoRandomInt(count int) int64 {
|
||||
nBig, err := rand.Int(rand.Reader, big.NewInt(int64(count)))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return nBig.Int64()
|
||||
}
|
||||
248
vendor/github.com/Masterminds/goutils/randomstringutils.go
generated
vendored
Normal file
248
vendor/github.com/Masterminds/goutils/randomstringutils.go
generated
vendored
Normal file
@@ -0,0 +1,248 @@
|
||||
/*
|
||||
Copyright 2014 Alexander Okoli
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package goutils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"math/rand"
|
||||
"time"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// RANDOM provides the time-based seed used to generate random numbers
|
||||
var RANDOM = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
|
||||
/*
|
||||
RandomNonAlphaNumeric creates a random string whose length is the number of characters specified.
|
||||
Characters will be chosen from the set of all characters (ASCII/Unicode values between 0 to 2,147,483,647 (math.MaxInt32)).
|
||||
|
||||
Parameter:
|
||||
count - the length of random string to create
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
|
||||
*/
|
||||
func RandomNonAlphaNumeric(count int) (string, error) {
|
||||
return RandomAlphaNumericCustom(count, false, false)
|
||||
}
|
||||
|
||||
/*
|
||||
RandomAscii creates a random string whose length is the number of characters specified.
|
||||
Characters will be chosen from the set of characters whose ASCII value is between 32 and 126 (inclusive).
|
||||
|
||||
Parameter:
|
||||
count - the length of random string to create
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
|
||||
*/
|
||||
func RandomAscii(count int) (string, error) {
|
||||
return Random(count, 32, 127, false, false)
|
||||
}
|
||||
|
||||
/*
|
||||
RandomNumeric creates a random string whose length is the number of characters specified.
|
||||
Characters will be chosen from the set of numeric characters.
|
||||
|
||||
Parameter:
|
||||
count - the length of random string to create
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
|
||||
*/
|
||||
func RandomNumeric(count int) (string, error) {
|
||||
return Random(count, 0, 0, false, true)
|
||||
}
|
||||
|
||||
/*
|
||||
RandomAlphabetic creates a random string whose length is the number of characters specified.
|
||||
Characters will be chosen from the set of alphabetic characters.
|
||||
|
||||
Parameters:
|
||||
count - the length of random string to create
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
|
||||
*/
|
||||
func RandomAlphabetic(count int) (string, error) {
|
||||
return Random(count, 0, 0, true, false)
|
||||
}
|
||||
|
||||
/*
|
||||
RandomAlphaNumeric creates a random string whose length is the number of characters specified.
|
||||
Characters will be chosen from the set of alpha-numeric characters.
|
||||
|
||||
Parameter:
|
||||
count - the length of random string to create
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
|
||||
*/
|
||||
func RandomAlphaNumeric(count int) (string, error) {
|
||||
return Random(count, 0, 0, true, true)
|
||||
}
|
||||
|
||||
/*
|
||||
RandomAlphaNumericCustom creates a random string whose length is the number of characters specified.
|
||||
Characters will be chosen from the set of alpha-numeric characters as indicated by the arguments.
|
||||
|
||||
Parameters:
|
||||
count - the length of random string to create
|
||||
letters - if true, generated string may include alphabetic characters
|
||||
numbers - if true, generated string may include numeric characters
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
|
||||
*/
|
||||
func RandomAlphaNumericCustom(count int, letters bool, numbers bool) (string, error) {
|
||||
return Random(count, 0, 0, letters, numbers)
|
||||
}
|
||||
|
||||
/*
|
||||
Random creates a random string based on a variety of options, using default source of randomness.
|
||||
This method has exactly the same semantics as RandomSeed(int, int, int, bool, bool, []char, *rand.Rand), but
|
||||
instead of using an externally supplied source of randomness, it uses the internal *rand.Rand instance.
|
||||
|
||||
Parameters:
|
||||
count - the length of random string to create
|
||||
start - the position in set of chars (ASCII/Unicode int) to start at
|
||||
end - the position in set of chars (ASCII/Unicode int) to end before
|
||||
letters - if true, generated string may include alphabetic characters
|
||||
numbers - if true, generated string may include numeric characters
|
||||
chars - the set of chars to choose randoms from. If nil, then it will use the set of all chars.
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
|
||||
*/
|
||||
func Random(count int, start int, end int, letters bool, numbers bool, chars ...rune) (string, error) {
|
||||
return RandomSeed(count, start, end, letters, numbers, chars, RANDOM)
|
||||
}
|
||||
|
||||
/*
|
||||
RandomSeed creates a random string based on a variety of options, using supplied source of randomness.
|
||||
If the parameters start and end are both 0, start and end are set to ' ' and 'z', the ASCII printable characters, will be used,
|
||||
unless letters and numbers are both false, in which case, start and end are set to 0 and math.MaxInt32, respectively.
|
||||
If chars is not nil, characters stored in chars that are between start and end are chosen.
|
||||
This method accepts a user-supplied *rand.Rand instance to use as a source of randomness. By seeding a single *rand.Rand instance
|
||||
with a fixed seed and using it for each call, the same random sequence of strings can be generated repeatedly and predictably.
|
||||
|
||||
Parameters:
|
||||
count - the length of random string to create
|
||||
start - the position in set of chars (ASCII/Unicode decimals) to start at
|
||||
end - the position in set of chars (ASCII/Unicode decimals) to end before
|
||||
letters - if true, generated string may include alphabetic characters
|
||||
numbers - if true, generated string may include numeric characters
|
||||
chars - the set of chars to choose randoms from. If nil, then it will use the set of all chars.
|
||||
random - a source of randomness.
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from invalid parameters: if count < 0; or the provided chars array is empty; or end <= start; or end > len(chars)
|
||||
*/
|
||||
func RandomSeed(count int, start int, end int, letters bool, numbers bool, chars []rune, random *rand.Rand) (string, error) {
|
||||
|
||||
if count == 0 {
|
||||
return "", nil
|
||||
} else if count < 0 {
|
||||
err := fmt.Errorf("randomstringutils illegal argument: Requested random string length %v is less than 0.", count) // equiv to err := errors.New("...")
|
||||
return "", err
|
||||
}
|
||||
if chars != nil && len(chars) == 0 {
|
||||
err := fmt.Errorf("randomstringutils illegal argument: The chars array must not be empty")
|
||||
return "", err
|
||||
}
|
||||
|
||||
if start == 0 && end == 0 {
|
||||
if chars != nil {
|
||||
end = len(chars)
|
||||
} else {
|
||||
if !letters && !numbers {
|
||||
end = math.MaxInt32
|
||||
} else {
|
||||
end = 'z' + 1
|
||||
start = ' '
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if end <= start {
|
||||
err := fmt.Errorf("randomstringutils illegal argument: Parameter end (%v) must be greater than start (%v)", end, start)
|
||||
return "", err
|
||||
}
|
||||
|
||||
if chars != nil && end > len(chars) {
|
||||
err := fmt.Errorf("randomstringutils illegal argument: Parameter end (%v) cannot be greater than len(chars) (%v)", end, len(chars))
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
buffer := make([]rune, count)
|
||||
gap := end - start
|
||||
|
||||
// high-surrogates range, (\uD800-\uDBFF) = 55296 - 56319
|
||||
// low-surrogates range, (\uDC00-\uDFFF) = 56320 - 57343
|
||||
|
||||
for count != 0 {
|
||||
count--
|
||||
var ch rune
|
||||
if chars == nil {
|
||||
ch = rune(random.Intn(gap) + start)
|
||||
} else {
|
||||
ch = chars[random.Intn(gap)+start]
|
||||
}
|
||||
|
||||
if letters && unicode.IsLetter(ch) || numbers && unicode.IsDigit(ch) || !letters && !numbers {
|
||||
if ch >= 56320 && ch <= 57343 { // low surrogate range
|
||||
if count == 0 {
|
||||
count++
|
||||
} else {
|
||||
// Insert low surrogate
|
||||
buffer[count] = ch
|
||||
count--
|
||||
// Insert high surrogate
|
||||
buffer[count] = rune(55296 + random.Intn(128))
|
||||
}
|
||||
} else if ch >= 55296 && ch <= 56191 { // High surrogates range (Partial)
|
||||
if count == 0 {
|
||||
count++
|
||||
} else {
|
||||
// Insert low surrogate
|
||||
buffer[count] = rune(56320 + random.Intn(128))
|
||||
count--
|
||||
// Insert high surrogate
|
||||
buffer[count] = ch
|
||||
}
|
||||
} else if ch >= 56192 && ch <= 56319 {
|
||||
// private high surrogate, skip it
|
||||
count++
|
||||
} else {
|
||||
// not one of the surrogates*
|
||||
buffer[count] = ch
|
||||
}
|
||||
} else {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return string(buffer), nil
|
||||
}
|
||||
240
vendor/github.com/Masterminds/goutils/stringutils.go
generated
vendored
Normal file
240
vendor/github.com/Masterminds/goutils/stringutils.go
generated
vendored
Normal file
@@ -0,0 +1,240 @@
|
||||
/*
|
||||
Copyright 2014 Alexander Okoli
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package goutils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// Typically returned by functions where a searched item cannot be found
|
||||
const INDEX_NOT_FOUND = -1
|
||||
|
||||
/*
|
||||
Abbreviate abbreviates a string using ellipses. This will turn the string "Now is the time for all good men" into "Now is the time for..."
|
||||
|
||||
Specifically, the algorithm is as follows:
|
||||
|
||||
- If str is less than maxWidth characters long, return it.
|
||||
- Else abbreviate it to (str[0:maxWidth - 3] + "...").
|
||||
- If maxWidth is less than 4, return an illegal argument error.
|
||||
- In no case will it return a string of length greater than maxWidth.
|
||||
|
||||
Parameters:
|
||||
str - the string to check
|
||||
maxWidth - maximum length of result string, must be at least 4
|
||||
|
||||
Returns:
|
||||
string - abbreviated string
|
||||
error - if the width is too small
|
||||
*/
|
||||
func Abbreviate(str string, maxWidth int) (string, error) {
|
||||
return AbbreviateFull(str, 0, maxWidth)
|
||||
}
|
||||
|
||||
/*
|
||||
AbbreviateFull abbreviates a string using ellipses. This will turn the string "Now is the time for all good men" into "...is the time for..."
|
||||
This function works like Abbreviate(string, int), but allows you to specify a "left edge" offset. Note that this left edge is not
|
||||
necessarily going to be the leftmost character in the result, or the first character following the ellipses, but it will appear
|
||||
somewhere in the result.
|
||||
In no case will it return a string of length greater than maxWidth.
|
||||
|
||||
Parameters:
|
||||
str - the string to check
|
||||
offset - left edge of source string
|
||||
maxWidth - maximum length of result string, must be at least 4
|
||||
|
||||
Returns:
|
||||
string - abbreviated string
|
||||
error - if the width is too small
|
||||
*/
|
||||
func AbbreviateFull(str string, offset int, maxWidth int) (string, error) {
|
||||
if str == "" {
|
||||
return "", nil
|
||||
}
|
||||
if maxWidth < 4 {
|
||||
err := fmt.Errorf("stringutils illegal argument: Minimum abbreviation width is 4")
|
||||
return "", err
|
||||
}
|
||||
if len(str) <= maxWidth {
|
||||
return str, nil
|
||||
}
|
||||
if offset > len(str) {
|
||||
offset = len(str)
|
||||
}
|
||||
if len(str)-offset < (maxWidth - 3) { // 15 - 5 < 10 - 3 = 10 < 7
|
||||
offset = len(str) - (maxWidth - 3)
|
||||
}
|
||||
abrevMarker := "..."
|
||||
if offset <= 4 {
|
||||
return str[0:maxWidth-3] + abrevMarker, nil // str.substring(0, maxWidth - 3) + abrevMarker;
|
||||
}
|
||||
if maxWidth < 7 {
|
||||
err := fmt.Errorf("stringutils illegal argument: Minimum abbreviation width with offset is 7")
|
||||
return "", err
|
||||
}
|
||||
if (offset + maxWidth - 3) < len(str) { // 5 + (10-3) < 15 = 12 < 15
|
||||
abrevStr, _ := Abbreviate(str[offset:len(str)], (maxWidth - 3))
|
||||
return abrevMarker + abrevStr, nil // abrevMarker + abbreviate(str.substring(offset), maxWidth - 3);
|
||||
}
|
||||
return abrevMarker + str[(len(str)-(maxWidth-3)):len(str)], nil // abrevMarker + str.substring(str.length() - (maxWidth - 3));
|
||||
}
|
||||
|
||||
/*
|
||||
DeleteWhiteSpace deletes all whitespaces from a string as defined by unicode.IsSpace(rune).
|
||||
It returns the string without whitespaces.
|
||||
|
||||
Parameter:
|
||||
str - the string to delete whitespace from, may be nil
|
||||
|
||||
Returns:
|
||||
the string without whitespaces
|
||||
*/
|
||||
func DeleteWhiteSpace(str string) string {
|
||||
if str == "" {
|
||||
return str
|
||||
}
|
||||
sz := len(str)
|
||||
var chs bytes.Buffer
|
||||
count := 0
|
||||
for i := 0; i < sz; i++ {
|
||||
ch := rune(str[i])
|
||||
if !unicode.IsSpace(ch) {
|
||||
chs.WriteRune(ch)
|
||||
count++
|
||||
}
|
||||
}
|
||||
if count == sz {
|
||||
return str
|
||||
}
|
||||
return chs.String()
|
||||
}
|
||||
|
||||
/*
|
||||
IndexOfDifference compares two strings, and returns the index at which the strings begin to differ.
|
||||
|
||||
Parameters:
|
||||
str1 - the first string
|
||||
str2 - the second string
|
||||
|
||||
Returns:
|
||||
the index where str1 and str2 begin to differ; -1 if they are equal
|
||||
*/
|
||||
func IndexOfDifference(str1 string, str2 string) int {
|
||||
if str1 == str2 {
|
||||
return INDEX_NOT_FOUND
|
||||
}
|
||||
if IsEmpty(str1) || IsEmpty(str2) {
|
||||
return 0
|
||||
}
|
||||
var i int
|
||||
for i = 0; i < len(str1) && i < len(str2); i++ {
|
||||
if rune(str1[i]) != rune(str2[i]) {
|
||||
break
|
||||
}
|
||||
}
|
||||
if i < len(str2) || i < len(str1) {
|
||||
return i
|
||||
}
|
||||
return INDEX_NOT_FOUND
|
||||
}
|
||||
|
||||
/*
|
||||
IsBlank checks if a string is whitespace or empty (""). Observe the following behavior:
|
||||
|
||||
goutils.IsBlank("") = true
|
||||
goutils.IsBlank(" ") = true
|
||||
goutils.IsBlank("bob") = false
|
||||
goutils.IsBlank(" bob ") = false
|
||||
|
||||
Parameter:
|
||||
str - the string to check
|
||||
|
||||
Returns:
|
||||
true - if the string is whitespace or empty ("")
|
||||
*/
|
||||
func IsBlank(str string) bool {
|
||||
strLen := len(str)
|
||||
if str == "" || strLen == 0 {
|
||||
return true
|
||||
}
|
||||
for i := 0; i < strLen; i++ {
|
||||
if unicode.IsSpace(rune(str[i])) == false {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/*
|
||||
IndexOf returns the index of the first instance of sub in str, with the search beginning from the
|
||||
index start point specified. -1 is returned if sub is not present in str.
|
||||
|
||||
An empty string ("") will return -1 (INDEX_NOT_FOUND). A negative start position is treated as zero.
|
||||
A start position greater than the string length returns -1.
|
||||
|
||||
Parameters:
|
||||
str - the string to check
|
||||
sub - the substring to find
|
||||
start - the start position; negative treated as zero
|
||||
|
||||
Returns:
|
||||
the first index where the sub string was found (always >= start)
|
||||
*/
|
||||
func IndexOf(str string, sub string, start int) int {
|
||||
|
||||
if start < 0 {
|
||||
start = 0
|
||||
}
|
||||
|
||||
if len(str) < start {
|
||||
return INDEX_NOT_FOUND
|
||||
}
|
||||
|
||||
if IsEmpty(str) || IsEmpty(sub) {
|
||||
return INDEX_NOT_FOUND
|
||||
}
|
||||
|
||||
partialIndex := strings.Index(str[start:len(str)], sub)
|
||||
if partialIndex == -1 {
|
||||
return INDEX_NOT_FOUND
|
||||
}
|
||||
return partialIndex + start
|
||||
}
|
||||
|
||||
// IsEmpty checks if a string is empty (""). Returns true if empty, and false otherwise.
|
||||
func IsEmpty(str string) bool {
|
||||
return len(str) == 0
|
||||
}
|
||||
|
||||
// Returns either the passed in string, or if the string is empty, the value of defaultStr.
|
||||
func DefaultString(str string, defaultStr string) string {
|
||||
if IsEmpty(str) {
|
||||
return defaultStr
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
// Returns either the passed in string, or if the string is whitespace, empty (""), the value of defaultStr.
|
||||
func DefaultIfBlank(str string, defaultStr string) string {
|
||||
if IsBlank(str) {
|
||||
return defaultStr
|
||||
}
|
||||
return str
|
||||
}
|
||||
357
vendor/github.com/Masterminds/goutils/wordutils.go
generated
vendored
Normal file
357
vendor/github.com/Masterminds/goutils/wordutils.go
generated
vendored
Normal file
@@ -0,0 +1,357 @@
|
||||
/*
|
||||
Copyright 2014 Alexander Okoli
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
/*
|
||||
Package goutils provides utility functions to manipulate strings in various ways.
|
||||
The code snippets below show examples of how to use goutils. Some functions return
|
||||
errors while others do not, so usage would vary as a result.
|
||||
|
||||
Example:
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/aokoli/goutils"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
// EXAMPLE 1: A goutils function which returns no errors
|
||||
fmt.Println (goutils.Initials("John Doe Foo")) // Prints out "JDF"
|
||||
|
||||
|
||||
|
||||
// EXAMPLE 2: A goutils function which returns an error
|
||||
rand1, err1 := goutils.Random (-1, 0, 0, true, true)
|
||||
|
||||
if err1 != nil {
|
||||
fmt.Println(err1) // Prints out error message because -1 was entered as the first parameter in goutils.Random(...)
|
||||
} else {
|
||||
fmt.Println(rand1)
|
||||
}
|
||||
}
|
||||
*/
|
||||
package goutils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// VERSION indicates the current version of goutils
|
||||
const VERSION = "1.0.0"
|
||||
|
||||
/*
|
||||
Wrap wraps a single line of text, identifying words by ' '.
|
||||
New lines will be separated by '\n'. Very long words, such as URLs will not be wrapped.
|
||||
Leading spaces on a new line are stripped. Trailing spaces are not stripped.
|
||||
|
||||
Parameters:
|
||||
str - the string to be word wrapped
|
||||
wrapLength - the column (a column can fit only one character) to wrap the words at, less than 1 is treated as 1
|
||||
|
||||
Returns:
|
||||
a line with newlines inserted
|
||||
*/
|
||||
func Wrap(str string, wrapLength int) string {
|
||||
return WrapCustom(str, wrapLength, "", false)
|
||||
}
|
||||
|
||||
/*
|
||||
WrapCustom wraps a single line of text, identifying words by ' '.
|
||||
Leading spaces on a new line are stripped. Trailing spaces are not stripped.
|
||||
|
||||
Parameters:
|
||||
str - the string to be word wrapped
|
||||
wrapLength - the column number (a column can fit only one character) to wrap the words at, less than 1 is treated as 1
|
||||
newLineStr - the string to insert for a new line, "" uses '\n'
|
||||
wrapLongWords - true if long words (such as URLs) should be wrapped
|
||||
|
||||
Returns:
|
||||
a line with newlines inserted
|
||||
*/
|
||||
func WrapCustom(str string, wrapLength int, newLineStr string, wrapLongWords bool) string {
|
||||
|
||||
if str == "" {
|
||||
return ""
|
||||
}
|
||||
if newLineStr == "" {
|
||||
newLineStr = "\n" // TODO Assumes "\n" is seperator. Explore SystemUtils.LINE_SEPARATOR from Apache Commons
|
||||
}
|
||||
if wrapLength < 1 {
|
||||
wrapLength = 1
|
||||
}
|
||||
|
||||
inputLineLength := len(str)
|
||||
offset := 0
|
||||
|
||||
var wrappedLine bytes.Buffer
|
||||
|
||||
for inputLineLength-offset > wrapLength {
|
||||
|
||||
if rune(str[offset]) == ' ' {
|
||||
offset++
|
||||
continue
|
||||
}
|
||||
|
||||
end := wrapLength + offset + 1
|
||||
spaceToWrapAt := strings.LastIndex(str[offset:end], " ") + offset
|
||||
|
||||
if spaceToWrapAt >= offset {
|
||||
// normal word (not longer than wrapLength)
|
||||
wrappedLine.WriteString(str[offset:spaceToWrapAt])
|
||||
wrappedLine.WriteString(newLineStr)
|
||||
offset = spaceToWrapAt + 1
|
||||
|
||||
} else {
|
||||
// long word or URL
|
||||
if wrapLongWords {
|
||||
end := wrapLength + offset
|
||||
// long words are wrapped one line at a time
|
||||
wrappedLine.WriteString(str[offset:end])
|
||||
wrappedLine.WriteString(newLineStr)
|
||||
offset += wrapLength
|
||||
} else {
|
||||
// long words aren't wrapped, just extended beyond limit
|
||||
end := wrapLength + offset
|
||||
index := strings.IndexRune(str[end:len(str)], ' ')
|
||||
if index == -1 {
|
||||
wrappedLine.WriteString(str[offset:len(str)])
|
||||
offset = inputLineLength
|
||||
} else {
|
||||
spaceToWrapAt = index + end
|
||||
wrappedLine.WriteString(str[offset:spaceToWrapAt])
|
||||
wrappedLine.WriteString(newLineStr)
|
||||
offset = spaceToWrapAt + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
wrappedLine.WriteString(str[offset:len(str)])
|
||||
|
||||
return wrappedLine.String()
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
Capitalize capitalizes all the delimiter separated words in a string. Only the first letter of each word is changed.
|
||||
To convert the rest of each word to lowercase at the same time, use CapitalizeFully(str string, delimiters ...rune).
|
||||
The delimiters represent a set of characters understood to separate words. The first string character
|
||||
and the first non-delimiter character after a delimiter will be capitalized. A "" input string returns "".
|
||||
Capitalization uses the Unicode title case, normally equivalent to upper case.
|
||||
|
||||
Parameters:
|
||||
str - the string to capitalize
|
||||
delimiters - set of characters to determine capitalization, exclusion of this parameter means whitespace would be delimeter
|
||||
|
||||
Returns:
|
||||
capitalized string
|
||||
*/
|
||||
func Capitalize(str string, delimiters ...rune) string {
|
||||
|
||||
var delimLen int
|
||||
|
||||
if delimiters == nil {
|
||||
delimLen = -1
|
||||
} else {
|
||||
delimLen = len(delimiters)
|
||||
}
|
||||
|
||||
if str == "" || delimLen == 0 {
|
||||
return str
|
||||
}
|
||||
|
||||
buffer := []rune(str)
|
||||
capitalizeNext := true
|
||||
for i := 0; i < len(buffer); i++ {
|
||||
ch := buffer[i]
|
||||
if isDelimiter(ch, delimiters...) {
|
||||
capitalizeNext = true
|
||||
} else if capitalizeNext {
|
||||
buffer[i] = unicode.ToTitle(ch)
|
||||
capitalizeNext = false
|
||||
}
|
||||
}
|
||||
return string(buffer)
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
CapitalizeFully converts all the delimiter separated words in a string into capitalized words, that is each word is made up of a
|
||||
titlecase character and then a series of lowercase characters. The delimiters represent a set of characters understood
|
||||
to separate words. The first string character and the first non-delimiter character after a delimiter will be capitalized.
|
||||
Capitalization uses the Unicode title case, normally equivalent to upper case.
|
||||
|
||||
Parameters:
|
||||
str - the string to capitalize fully
|
||||
delimiters - set of characters to determine capitalization, exclusion of this parameter means whitespace would be delimeter
|
||||
|
||||
Returns:
|
||||
capitalized string
|
||||
*/
|
||||
func CapitalizeFully(str string, delimiters ...rune) string {
|
||||
|
||||
var delimLen int
|
||||
|
||||
if delimiters == nil {
|
||||
delimLen = -1
|
||||
} else {
|
||||
delimLen = len(delimiters)
|
||||
}
|
||||
|
||||
if str == "" || delimLen == 0 {
|
||||
return str
|
||||
}
|
||||
str = strings.ToLower(str)
|
||||
return Capitalize(str, delimiters...)
|
||||
}
|
||||
|
||||
/*
|
||||
Uncapitalize uncapitalizes all the whitespace separated words in a string. Only the first letter of each word is changed.
|
||||
The delimiters represent a set of characters understood to separate words. The first string character and the first non-delimiter
|
||||
character after a delimiter will be uncapitalized. Whitespace is defined by unicode.IsSpace(char).
|
||||
|
||||
Parameters:
|
||||
str - the string to uncapitalize fully
|
||||
delimiters - set of characters to determine capitalization, exclusion of this parameter means whitespace would be delimeter
|
||||
|
||||
Returns:
|
||||
uncapitalized string
|
||||
*/
|
||||
func Uncapitalize(str string, delimiters ...rune) string {
|
||||
|
||||
var delimLen int
|
||||
|
||||
if delimiters == nil {
|
||||
delimLen = -1
|
||||
} else {
|
||||
delimLen = len(delimiters)
|
||||
}
|
||||
|
||||
if str == "" || delimLen == 0 {
|
||||
return str
|
||||
}
|
||||
|
||||
buffer := []rune(str)
|
||||
uncapitalizeNext := true // TODO Always makes capitalize/un apply to first char.
|
||||
for i := 0; i < len(buffer); i++ {
|
||||
ch := buffer[i]
|
||||
if isDelimiter(ch, delimiters...) {
|
||||
uncapitalizeNext = true
|
||||
} else if uncapitalizeNext {
|
||||
buffer[i] = unicode.ToLower(ch)
|
||||
uncapitalizeNext = false
|
||||
}
|
||||
}
|
||||
return string(buffer)
|
||||
}
|
||||
|
||||
/*
|
||||
SwapCase swaps the case of a string using a word based algorithm.
|
||||
|
||||
Conversion algorithm:
|
||||
|
||||
Upper case character converts to Lower case
|
||||
Title case character converts to Lower case
|
||||
Lower case character after Whitespace or at start converts to Title case
|
||||
Other Lower case character converts to Upper case
|
||||
Whitespace is defined by unicode.IsSpace(char).
|
||||
|
||||
Parameters:
|
||||
str - the string to swap case
|
||||
|
||||
Returns:
|
||||
the changed string
|
||||
*/
|
||||
func SwapCase(str string) string {
|
||||
if str == "" {
|
||||
return str
|
||||
}
|
||||
buffer := []rune(str)
|
||||
|
||||
whitespace := true
|
||||
|
||||
for i := 0; i < len(buffer); i++ {
|
||||
ch := buffer[i]
|
||||
if unicode.IsUpper(ch) {
|
||||
buffer[i] = unicode.ToLower(ch)
|
||||
whitespace = false
|
||||
} else if unicode.IsTitle(ch) {
|
||||
buffer[i] = unicode.ToLower(ch)
|
||||
whitespace = false
|
||||
} else if unicode.IsLower(ch) {
|
||||
if whitespace {
|
||||
buffer[i] = unicode.ToTitle(ch)
|
||||
whitespace = false
|
||||
} else {
|
||||
buffer[i] = unicode.ToUpper(ch)
|
||||
}
|
||||
} else {
|
||||
whitespace = unicode.IsSpace(ch)
|
||||
}
|
||||
}
|
||||
return string(buffer)
|
||||
}
|
||||
|
||||
/*
|
||||
Initials extracts the initial letters from each word in the string. The first letter of the string and all first
|
||||
letters after the defined delimiters are returned as a new string. Their case is not changed. If the delimiters
|
||||
parameter is excluded, then Whitespace is used. Whitespace is defined by unicode.IsSpacea(char). An empty delimiter array returns an empty string.
|
||||
|
||||
Parameters:
|
||||
str - the string to get initials from
|
||||
delimiters - set of characters to determine words, exclusion of this parameter means whitespace would be delimeter
|
||||
Returns:
|
||||
string of initial letters
|
||||
*/
|
||||
func Initials(str string, delimiters ...rune) string {
|
||||
if str == "" {
|
||||
return str
|
||||
}
|
||||
if delimiters != nil && len(delimiters) == 0 {
|
||||
return ""
|
||||
}
|
||||
strLen := len(str)
|
||||
var buf bytes.Buffer
|
||||
lastWasGap := true
|
||||
for i := 0; i < strLen; i++ {
|
||||
ch := rune(str[i])
|
||||
|
||||
if isDelimiter(ch, delimiters...) {
|
||||
lastWasGap = true
|
||||
} else if lastWasGap {
|
||||
buf.WriteRune(ch)
|
||||
lastWasGap = false
|
||||
}
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// private function (lower case func name)
|
||||
func isDelimiter(ch rune, delimiters ...rune) bool {
|
||||
if delimiters == nil {
|
||||
return unicode.IsSpace(ch)
|
||||
}
|
||||
for _, delimiter := range delimiters {
|
||||
if ch == delimiter {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
1
vendor/github.com/Masterminds/semver/v3/.gitignore
generated
vendored
Normal file
1
vendor/github.com/Masterminds/semver/v3/.gitignore
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
_fuzz/
|
||||
30
vendor/github.com/Masterminds/semver/v3/.golangci.yml
generated
vendored
Normal file
30
vendor/github.com/Masterminds/semver/v3/.golangci.yml
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
run:
|
||||
deadline: 2m
|
||||
|
||||
linters:
|
||||
disable-all: true
|
||||
enable:
|
||||
- misspell
|
||||
- structcheck
|
||||
- govet
|
||||
- staticcheck
|
||||
- deadcode
|
||||
- errcheck
|
||||
- varcheck
|
||||
- unparam
|
||||
- ineffassign
|
||||
- nakedret
|
||||
- gocyclo
|
||||
- dupl
|
||||
- goimports
|
||||
- revive
|
||||
- gosec
|
||||
- gosimple
|
||||
- typecheck
|
||||
- unused
|
||||
|
||||
linters-settings:
|
||||
gofmt:
|
||||
simplify: true
|
||||
dupl:
|
||||
threshold: 600
|
||||
214
vendor/github.com/Masterminds/semver/v3/CHANGELOG.md
generated
vendored
Normal file
214
vendor/github.com/Masterminds/semver/v3/CHANGELOG.md
generated
vendored
Normal file
@@ -0,0 +1,214 @@
|
||||
# Changelog
|
||||
|
||||
## 3.2.0 (2022-11-28)
|
||||
|
||||
### Added
|
||||
|
||||
- #190: Added text marshaling and unmarshaling
|
||||
- #167: Added JSON marshalling for constraints (thanks @SimonTheLeg)
|
||||
- #173: Implement encoding.TextMarshaler and encoding.TextUnmarshaler on Version (thanks @MarkRosemaker)
|
||||
- #179: Added New() version constructor (thanks @kazhuravlev)
|
||||
|
||||
### Changed
|
||||
|
||||
- #182/#183: Updated CI testing setup
|
||||
|
||||
### Fixed
|
||||
|
||||
- #186: Fixing issue where validation of constraint section gave false positives
|
||||
- #176: Fix constraints check with *-0 (thanks @mtt0)
|
||||
- #181: Fixed Caret operator (^) gives unexpected results when the minor version in constraint is 0 (thanks @arshchimni)
|
||||
- #161: Fixed godoc (thanks @afirth)
|
||||
|
||||
## 3.1.1 (2020-11-23)
|
||||
|
||||
### Fixed
|
||||
|
||||
- #158: Fixed issue with generated regex operation order that could cause problem
|
||||
|
||||
## 3.1.0 (2020-04-15)
|
||||
|
||||
### Added
|
||||
|
||||
- #131: Add support for serializing/deserializing SQL (thanks @ryancurrah)
|
||||
|
||||
### Changed
|
||||
|
||||
- #148: More accurate validation messages on constraints
|
||||
|
||||
## 3.0.3 (2019-12-13)
|
||||
|
||||
### Fixed
|
||||
|
||||
- #141: Fixed issue with <= comparison
|
||||
|
||||
## 3.0.2 (2019-11-14)
|
||||
|
||||
### Fixed
|
||||
|
||||
- #134: Fixed broken constraint checking with ^0.0 (thanks @krmichelos)
|
||||
|
||||
## 3.0.1 (2019-09-13)
|
||||
|
||||
### Fixed
|
||||
|
||||
- #125: Fixes issue with module path for v3
|
||||
|
||||
## 3.0.0 (2019-09-12)
|
||||
|
||||
This is a major release of the semver package which includes API changes. The Go
|
||||
API is compatible with ^1. The Go API was not changed because many people are using
|
||||
`go get` without Go modules for their applications and API breaking changes cause
|
||||
errors which we have or would need to support.
|
||||
|
||||
The changes in this release are the handling based on the data passed into the
|
||||
functions. These are described in the added and changed sections below.
|
||||
|
||||
### Added
|
||||
|
||||
- StrictNewVersion function. This is similar to NewVersion but will return an
|
||||
error if the version passed in is not a strict semantic version. For example,
|
||||
1.2.3 would pass but v1.2.3 or 1.2 would fail because they are not strictly
|
||||
speaking semantic versions. This function is faster, performs fewer operations,
|
||||
and uses fewer allocations than NewVersion.
|
||||
- Fuzzing has been performed on NewVersion, StrictNewVersion, and NewConstraint.
|
||||
The Makefile contains the operations used. For more information on you can start
|
||||
on Wikipedia at https://en.wikipedia.org/wiki/Fuzzing
|
||||
- Now using Go modules
|
||||
|
||||
### Changed
|
||||
|
||||
- NewVersion has proper prerelease and metadata validation with error messages
|
||||
to signal an issue with either of them
|
||||
- ^ now operates using a similar set of rules to npm/js and Rust/Cargo. If the
|
||||
version is >=1 the ^ ranges works the same as v1. For major versions of 0 the
|
||||
rules have changed. The minor version is treated as the stable version unless
|
||||
a patch is specified and then it is equivalent to =. One difference from npm/js
|
||||
is that prereleases there are only to a specific version (e.g. 1.2.3).
|
||||
Prereleases here look over multiple versions and follow semantic version
|
||||
ordering rules. This pattern now follows along with the expected and requested
|
||||
handling of this packaged by numerous users.
|
||||
|
||||
## 1.5.0 (2019-09-11)
|
||||
|
||||
### Added
|
||||
|
||||
- #103: Add basic fuzzing for `NewVersion()` (thanks @jesse-c)
|
||||
|
||||
### Changed
|
||||
|
||||
- #82: Clarify wildcard meaning in range constraints and update tests for it (thanks @greysteil)
|
||||
- #83: Clarify caret operator range for pre-1.0.0 dependencies (thanks @greysteil)
|
||||
- #72: Adding docs comment pointing to vert for a cli
|
||||
- #71: Update the docs on pre-release comparator handling
|
||||
- #89: Test with new go versions (thanks @thedevsaddam)
|
||||
- #87: Added $ to ValidPrerelease for better validation (thanks @jeremycarroll)
|
||||
|
||||
### Fixed
|
||||
|
||||
- #78: Fix unchecked error in example code (thanks @ravron)
|
||||
- #70: Fix the handling of pre-releases and the 0.0.0 release edge case
|
||||
- #97: Fixed copyright file for proper display on GitHub
|
||||
- #107: Fix handling prerelease when sorting alphanum and num
|
||||
- #109: Fixed where Validate sometimes returns wrong message on error
|
||||
|
||||
## 1.4.2 (2018-04-10)
|
||||
|
||||
### Changed
|
||||
|
||||
- #72: Updated the docs to point to vert for a console appliaction
|
||||
- #71: Update the docs on pre-release comparator handling
|
||||
|
||||
### Fixed
|
||||
|
||||
- #70: Fix the handling of pre-releases and the 0.0.0 release edge case
|
||||
|
||||
## 1.4.1 (2018-04-02)
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fixed #64: Fix pre-release precedence issue (thanks @uudashr)
|
||||
|
||||
## 1.4.0 (2017-10-04)
|
||||
|
||||
### Changed
|
||||
|
||||
- #61: Update NewVersion to parse ints with a 64bit int size (thanks @zknill)
|
||||
|
||||
## 1.3.1 (2017-07-10)
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fixed #57: number comparisons in prerelease sometimes inaccurate
|
||||
|
||||
## 1.3.0 (2017-05-02)
|
||||
|
||||
### Added
|
||||
|
||||
- #45: Added json (un)marshaling support (thanks @mh-cbon)
|
||||
- Stability marker. See https://masterminds.github.io/stability/
|
||||
|
||||
### Fixed
|
||||
|
||||
- #51: Fix handling of single digit tilde constraint (thanks @dgodd)
|
||||
|
||||
### Changed
|
||||
|
||||
- #55: The godoc icon moved from png to svg
|
||||
|
||||
## 1.2.3 (2017-04-03)
|
||||
|
||||
### Fixed
|
||||
|
||||
- #46: Fixed 0.x.x and 0.0.x in constraints being treated as *
|
||||
|
||||
## Release 1.2.2 (2016-12-13)
|
||||
|
||||
### Fixed
|
||||
|
||||
- #34: Fixed issue where hyphen range was not working with pre-release parsing.
|
||||
|
||||
## Release 1.2.1 (2016-11-28)
|
||||
|
||||
### Fixed
|
||||
|
||||
- #24: Fixed edge case issue where constraint "> 0" does not handle "0.0.1-alpha"
|
||||
properly.
|
||||
|
||||
## Release 1.2.0 (2016-11-04)
|
||||
|
||||
### Added
|
||||
|
||||
- #20: Added MustParse function for versions (thanks @adamreese)
|
||||
- #15: Added increment methods on versions (thanks @mh-cbon)
|
||||
|
||||
### Fixed
|
||||
|
||||
- Issue #21: Per the SemVer spec (section 9) a pre-release is unstable and
|
||||
might not satisfy the intended compatibility. The change here ignores pre-releases
|
||||
on constraint checks (e.g., ~ or ^) when a pre-release is not part of the
|
||||
constraint. For example, `^1.2.3` will ignore pre-releases while
|
||||
`^1.2.3-alpha` will include them.
|
||||
|
||||
## Release 1.1.1 (2016-06-30)
|
||||
|
||||
### Changed
|
||||
|
||||
- Issue #9: Speed up version comparison performance (thanks @sdboyer)
|
||||
- Issue #8: Added benchmarks (thanks @sdboyer)
|
||||
- Updated Go Report Card URL to new location
|
||||
- Updated Readme to add code snippet formatting (thanks @mh-cbon)
|
||||
- Updating tagging to v[SemVer] structure for compatibility with other tools.
|
||||
|
||||
## Release 1.1.0 (2016-03-11)
|
||||
|
||||
- Issue #2: Implemented validation to provide reasons a versions failed a
|
||||
constraint.
|
||||
|
||||
## Release 1.0.1 (2015-12-31)
|
||||
|
||||
- Fixed #1: * constraint failing on valid versions.
|
||||
|
||||
## Release 1.0.0 (2015-10-20)
|
||||
|
||||
- Initial release
|
||||
19
vendor/github.com/Masterminds/semver/v3/LICENSE.txt
generated
vendored
Normal file
19
vendor/github.com/Masterminds/semver/v3/LICENSE.txt
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
Copyright (C) 2014-2019, Matt Butcher and Matt Farina
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
37
vendor/github.com/Masterminds/semver/v3/Makefile
generated
vendored
Normal file
37
vendor/github.com/Masterminds/semver/v3/Makefile
generated
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
GOPATH=$(shell go env GOPATH)
|
||||
GOLANGCI_LINT=$(GOPATH)/bin/golangci-lint
|
||||
GOFUZZBUILD = $(GOPATH)/bin/go-fuzz-build
|
||||
GOFUZZ = $(GOPATH)/bin/go-fuzz
|
||||
|
||||
.PHONY: lint
|
||||
lint: $(GOLANGCI_LINT)
|
||||
@echo "==> Linting codebase"
|
||||
@$(GOLANGCI_LINT) run
|
||||
|
||||
.PHONY: test
|
||||
test:
|
||||
@echo "==> Running tests"
|
||||
GO111MODULE=on go test -v
|
||||
|
||||
.PHONY: test-cover
|
||||
test-cover:
|
||||
@echo "==> Running Tests with coverage"
|
||||
GO111MODULE=on go test -cover .
|
||||
|
||||
.PHONY: fuzz
|
||||
fuzz: $(GOFUZZBUILD) $(GOFUZZ)
|
||||
@echo "==> Fuzz testing"
|
||||
$(GOFUZZBUILD)
|
||||
$(GOFUZZ) -workdir=_fuzz
|
||||
|
||||
$(GOLANGCI_LINT):
|
||||
# Install golangci-lint. The configuration for it is in the .golangci.yml
|
||||
# file in the root of the repository
|
||||
echo ${GOPATH}
|
||||
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(GOPATH)/bin v1.17.1
|
||||
|
||||
$(GOFUZZBUILD):
|
||||
cd / && go get -u github.com/dvyukov/go-fuzz/go-fuzz-build
|
||||
|
||||
$(GOFUZZ):
|
||||
cd / && go get -u github.com/dvyukov/go-fuzz/go-fuzz github.com/dvyukov/go-fuzz/go-fuzz-dep
|
||||
244
vendor/github.com/Masterminds/semver/v3/README.md
generated
vendored
Normal file
244
vendor/github.com/Masterminds/semver/v3/README.md
generated
vendored
Normal file
@@ -0,0 +1,244 @@
|
||||
# SemVer
|
||||
|
||||
The `semver` package provides the ability to work with [Semantic Versions](http://semver.org) in Go. Specifically it provides the ability to:
|
||||
|
||||
* Parse semantic versions
|
||||
* Sort semantic versions
|
||||
* Check if a semantic version fits within a set of constraints
|
||||
* Optionally work with a `v` prefix
|
||||
|
||||
[](https://masterminds.github.io/stability/active.html)
|
||||
[](https://github.com/Masterminds/semver/actions)
|
||||
[](https://pkg.go.dev/github.com/Masterminds/semver/v3)
|
||||
[](https://goreportcard.com/report/github.com/Masterminds/semver)
|
||||
|
||||
If you are looking for a command line tool for version comparisons please see
|
||||
[vert](https://github.com/Masterminds/vert) which uses this library.
|
||||
|
||||
## Package Versions
|
||||
|
||||
There are three major versions fo the `semver` package.
|
||||
|
||||
* 3.x.x is the new stable and active version. This version is focused on constraint
|
||||
compatibility for range handling in other tools from other languages. It has
|
||||
a similar API to the v1 releases. The development of this version is on the master
|
||||
branch. The documentation for this version is below.
|
||||
* 2.x was developed primarily for [dep](https://github.com/golang/dep). There are
|
||||
no tagged releases and the development was performed by [@sdboyer](https://github.com/sdboyer).
|
||||
There are API breaking changes from v1. This version lives on the [2.x branch](https://github.com/Masterminds/semver/tree/2.x).
|
||||
* 1.x.x is the most widely used version with numerous tagged releases. This is the
|
||||
previous stable and is still maintained for bug fixes. The development, to fix
|
||||
bugs, occurs on the release-1 branch. You can read the documentation [here](https://github.com/Masterminds/semver/blob/release-1/README.md).
|
||||
|
||||
## Parsing Semantic Versions
|
||||
|
||||
There are two functions that can parse semantic versions. The `StrictNewVersion`
|
||||
function only parses valid version 2 semantic versions as outlined in the
|
||||
specification. The `NewVersion` function attempts to coerce a version into a
|
||||
semantic version and parse it. For example, if there is a leading v or a version
|
||||
listed without all 3 parts (e.g. `v1.2`) it will attempt to coerce it into a valid
|
||||
semantic version (e.g., 1.2.0). In both cases a `Version` object is returned
|
||||
that can be sorted, compared, and used in constraints.
|
||||
|
||||
When parsing a version an error is returned if there is an issue parsing the
|
||||
version. For example,
|
||||
|
||||
v, err := semver.NewVersion("1.2.3-beta.1+build345")
|
||||
|
||||
The version object has methods to get the parts of the version, compare it to
|
||||
other versions, convert the version back into a string, and get the original
|
||||
string. Getting the original string is useful if the semantic version was coerced
|
||||
into a valid form.
|
||||
|
||||
## Sorting Semantic Versions
|
||||
|
||||
A set of versions can be sorted using the `sort` package from the standard library.
|
||||
For example,
|
||||
|
||||
```go
|
||||
raw := []string{"1.2.3", "1.0", "1.3", "2", "0.4.2",}
|
||||
vs := make([]*semver.Version, len(raw))
|
||||
for i, r := range raw {
|
||||
v, err := semver.NewVersion(r)
|
||||
if err != nil {
|
||||
t.Errorf("Error parsing version: %s", err)
|
||||
}
|
||||
|
||||
vs[i] = v
|
||||
}
|
||||
|
||||
sort.Sort(semver.Collection(vs))
|
||||
```
|
||||
|
||||
## Checking Version Constraints
|
||||
|
||||
There are two methods for comparing versions. One uses comparison methods on
|
||||
`Version` instances and the other uses `Constraints`. There are some important
|
||||
differences to notes between these two methods of comparison.
|
||||
|
||||
1. When two versions are compared using functions such as `Compare`, `LessThan`,
|
||||
and others it will follow the specification and always include prereleases
|
||||
within the comparison. It will provide an answer that is valid with the
|
||||
comparison section of the spec at https://semver.org/#spec-item-11
|
||||
2. When constraint checking is used for checks or validation it will follow a
|
||||
different set of rules that are common for ranges with tools like npm/js
|
||||
and Rust/Cargo. This includes considering prereleases to be invalid if the
|
||||
ranges does not include one. If you want to have it include pre-releases a
|
||||
simple solution is to include `-0` in your range.
|
||||
3. Constraint ranges can have some complex rules including the shorthand use of
|
||||
~ and ^. For more details on those see the options below.
|
||||
|
||||
There are differences between the two methods or checking versions because the
|
||||
comparison methods on `Version` follow the specification while comparison ranges
|
||||
are not part of the specification. Different packages and tools have taken it
|
||||
upon themselves to come up with range rules. This has resulted in differences.
|
||||
For example, npm/js and Cargo/Rust follow similar patterns while PHP has a
|
||||
different pattern for ^. The comparison features in this package follow the
|
||||
npm/js and Cargo/Rust lead because applications using it have followed similar
|
||||
patters with their versions.
|
||||
|
||||
Checking a version against version constraints is one of the most featureful
|
||||
parts of the package.
|
||||
|
||||
```go
|
||||
c, err := semver.NewConstraint(">= 1.2.3")
|
||||
if err != nil {
|
||||
// Handle constraint not being parsable.
|
||||
}
|
||||
|
||||
v, err := semver.NewVersion("1.3")
|
||||
if err != nil {
|
||||
// Handle version not being parsable.
|
||||
}
|
||||
// Check if the version meets the constraints. The a variable will be true.
|
||||
a := c.Check(v)
|
||||
```
|
||||
|
||||
### Basic Comparisons
|
||||
|
||||
There are two elements to the comparisons. First, a comparison string is a list
|
||||
of space or comma separated AND comparisons. These are then separated by || (OR)
|
||||
comparisons. For example, `">= 1.2 < 3.0.0 || >= 4.2.3"` is looking for a
|
||||
comparison that's greater than or equal to 1.2 and less than 3.0.0 or is
|
||||
greater than or equal to 4.2.3.
|
||||
|
||||
The basic comparisons are:
|
||||
|
||||
* `=`: equal (aliased to no operator)
|
||||
* `!=`: not equal
|
||||
* `>`: greater than
|
||||
* `<`: less than
|
||||
* `>=`: greater than or equal to
|
||||
* `<=`: less than or equal to
|
||||
|
||||
### Working With Prerelease Versions
|
||||
|
||||
Pre-releases, for those not familiar with them, are used for software releases
|
||||
prior to stable or generally available releases. Examples of prereleases include
|
||||
development, alpha, beta, and release candidate releases. A prerelease may be
|
||||
a version such as `1.2.3-beta.1` while the stable release would be `1.2.3`. In the
|
||||
order of precedence, prereleases come before their associated releases. In this
|
||||
example `1.2.3-beta.1 < 1.2.3`.
|
||||
|
||||
According to the Semantic Version specification prereleases may not be
|
||||
API compliant with their release counterpart. It says,
|
||||
|
||||
> A pre-release version indicates that the version is unstable and might not satisfy the intended compatibility requirements as denoted by its associated normal version.
|
||||
|
||||
SemVer comparisons using constraints without a prerelease comparator will skip
|
||||
prerelease versions. For example, `>=1.2.3` will skip prereleases when looking
|
||||
at a list of releases while `>=1.2.3-0` will evaluate and find prereleases.
|
||||
|
||||
The reason for the `0` as a pre-release version in the example comparison is
|
||||
because pre-releases can only contain ASCII alphanumerics and hyphens (along with
|
||||
`.` separators), per the spec. Sorting happens in ASCII sort order, again per the
|
||||
spec. The lowest character is a `0` in ASCII sort order
|
||||
(see an [ASCII Table](http://www.asciitable.com/))
|
||||
|
||||
Understanding ASCII sort ordering is important because A-Z comes before a-z. That
|
||||
means `>=1.2.3-BETA` will return `1.2.3-alpha`. What you might expect from case
|
||||
sensitivity doesn't apply here. This is due to ASCII sort ordering which is what
|
||||
the spec specifies.
|
||||
|
||||
### Hyphen Range Comparisons
|
||||
|
||||
There are multiple methods to handle ranges and the first is hyphens ranges.
|
||||
These look like:
|
||||
|
||||
* `1.2 - 1.4.5` which is equivalent to `>= 1.2 <= 1.4.5`
|
||||
* `2.3.4 - 4.5` which is equivalent to `>= 2.3.4 <= 4.5`
|
||||
|
||||
### Wildcards In Comparisons
|
||||
|
||||
The `x`, `X`, and `*` characters can be used as a wildcard character. This works
|
||||
for all comparison operators. When used on the `=` operator it falls
|
||||
back to the patch level comparison (see tilde below). For example,
|
||||
|
||||
* `1.2.x` is equivalent to `>= 1.2.0, < 1.3.0`
|
||||
* `>= 1.2.x` is equivalent to `>= 1.2.0`
|
||||
* `<= 2.x` is equivalent to `< 3`
|
||||
* `*` is equivalent to `>= 0.0.0`
|
||||
|
||||
### Tilde Range Comparisons (Patch)
|
||||
|
||||
The tilde (`~`) comparison operator is for patch level ranges when a minor
|
||||
version is specified and major level changes when the minor number is missing.
|
||||
For example,
|
||||
|
||||
* `~1.2.3` is equivalent to `>= 1.2.3, < 1.3.0`
|
||||
* `~1` is equivalent to `>= 1, < 2`
|
||||
* `~2.3` is equivalent to `>= 2.3, < 2.4`
|
||||
* `~1.2.x` is equivalent to `>= 1.2.0, < 1.3.0`
|
||||
* `~1.x` is equivalent to `>= 1, < 2`
|
||||
|
||||
### Caret Range Comparisons (Major)
|
||||
|
||||
The caret (`^`) comparison operator is for major level changes once a stable
|
||||
(1.0.0) release has occurred. Prior to a 1.0.0 release the minor versions acts
|
||||
as the API stability level. This is useful when comparisons of API versions as a
|
||||
major change is API breaking. For example,
|
||||
|
||||
* `^1.2.3` is equivalent to `>= 1.2.3, < 2.0.0`
|
||||
* `^1.2.x` is equivalent to `>= 1.2.0, < 2.0.0`
|
||||
* `^2.3` is equivalent to `>= 2.3, < 3`
|
||||
* `^2.x` is equivalent to `>= 2.0.0, < 3`
|
||||
* `^0.2.3` is equivalent to `>=0.2.3 <0.3.0`
|
||||
* `^0.2` is equivalent to `>=0.2.0 <0.3.0`
|
||||
* `^0.0.3` is equivalent to `>=0.0.3 <0.0.4`
|
||||
* `^0.0` is equivalent to `>=0.0.0 <0.1.0`
|
||||
* `^0` is equivalent to `>=0.0.0 <1.0.0`
|
||||
|
||||
## Validation
|
||||
|
||||
In addition to testing a version against a constraint, a version can be validated
|
||||
against a constraint. When validation fails a slice of errors containing why a
|
||||
version didn't meet the constraint is returned. For example,
|
||||
|
||||
```go
|
||||
c, err := semver.NewConstraint("<= 1.2.3, >= 1.4")
|
||||
if err != nil {
|
||||
// Handle constraint not being parseable.
|
||||
}
|
||||
|
||||
v, err := semver.NewVersion("1.3")
|
||||
if err != nil {
|
||||
// Handle version not being parseable.
|
||||
}
|
||||
|
||||
// Validate a version against a constraint.
|
||||
a, msgs := c.Validate(v)
|
||||
// a is false
|
||||
for _, m := range msgs {
|
||||
fmt.Println(m)
|
||||
|
||||
// Loops over the errors which would read
|
||||
// "1.3 is greater than 1.2.3"
|
||||
// "1.3 is less than 1.4"
|
||||
}
|
||||
```
|
||||
|
||||
## Contribute
|
||||
|
||||
If you find an issue or want to contribute please file an [issue](https://github.com/Masterminds/semver/issues)
|
||||
or [create a pull request](https://github.com/Masterminds/semver/pulls).
|
||||
24
vendor/github.com/Masterminds/semver/v3/collection.go
generated
vendored
Normal file
24
vendor/github.com/Masterminds/semver/v3/collection.go
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
package semver
|
||||
|
||||
// Collection is a collection of Version instances and implements the sort
|
||||
// interface. See the sort package for more details.
|
||||
// https://golang.org/pkg/sort/
|
||||
type Collection []*Version
|
||||
|
||||
// Len returns the length of a collection. The number of Version instances
|
||||
// on the slice.
|
||||
func (c Collection) Len() int {
|
||||
return len(c)
|
||||
}
|
||||
|
||||
// Less is needed for the sort interface to compare two Version objects on the
|
||||
// slice. If checks if one is less than the other.
|
||||
func (c Collection) Less(i, j int) bool {
|
||||
return c[i].LessThan(c[j])
|
||||
}
|
||||
|
||||
// Swap is needed for the sort interface to replace the Version objects
|
||||
// at two different positions in the slice.
|
||||
func (c Collection) Swap(i, j int) {
|
||||
c[i], c[j] = c[j], c[i]
|
||||
}
|
||||
594
vendor/github.com/Masterminds/semver/v3/constraints.go
generated
vendored
Normal file
594
vendor/github.com/Masterminds/semver/v3/constraints.go
generated
vendored
Normal file
@@ -0,0 +1,594 @@
|
||||
package semver
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Constraints is one or more constraint that a semantic version can be
|
||||
// checked against.
|
||||
type Constraints struct {
|
||||
constraints [][]*constraint
|
||||
}
|
||||
|
||||
// NewConstraint returns a Constraints instance that a Version instance can
|
||||
// be checked against. If there is a parse error it will be returned.
|
||||
func NewConstraint(c string) (*Constraints, error) {
|
||||
|
||||
// Rewrite - ranges into a comparison operation.
|
||||
c = rewriteRange(c)
|
||||
|
||||
ors := strings.Split(c, "||")
|
||||
or := make([][]*constraint, len(ors))
|
||||
for k, v := range ors {
|
||||
|
||||
// TODO: Find a way to validate and fetch all the constraints in a simpler form
|
||||
|
||||
// Validate the segment
|
||||
if !validConstraintRegex.MatchString(v) {
|
||||
return nil, fmt.Errorf("improper constraint: %s", v)
|
||||
}
|
||||
|
||||
cs := findConstraintRegex.FindAllString(v, -1)
|
||||
if cs == nil {
|
||||
cs = append(cs, v)
|
||||
}
|
||||
result := make([]*constraint, len(cs))
|
||||
for i, s := range cs {
|
||||
pc, err := parseConstraint(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result[i] = pc
|
||||
}
|
||||
or[k] = result
|
||||
}
|
||||
|
||||
o := &Constraints{constraints: or}
|
||||
return o, nil
|
||||
}
|
||||
|
||||
// Check tests if a version satisfies the constraints.
|
||||
func (cs Constraints) Check(v *Version) bool {
|
||||
// TODO(mattfarina): For v4 of this library consolidate the Check and Validate
|
||||
// functions as the underlying functions make that possible now.
|
||||
// loop over the ORs and check the inner ANDs
|
||||
for _, o := range cs.constraints {
|
||||
joy := true
|
||||
for _, c := range o {
|
||||
if check, _ := c.check(v); !check {
|
||||
joy = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if joy {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Validate checks if a version satisfies a constraint. If not a slice of
|
||||
// reasons for the failure are returned in addition to a bool.
|
||||
func (cs Constraints) Validate(v *Version) (bool, []error) {
|
||||
// loop over the ORs and check the inner ANDs
|
||||
var e []error
|
||||
|
||||
// Capture the prerelease message only once. When it happens the first time
|
||||
// this var is marked
|
||||
var prerelesase bool
|
||||
for _, o := range cs.constraints {
|
||||
joy := true
|
||||
for _, c := range o {
|
||||
// Before running the check handle the case there the version is
|
||||
// a prerelease and the check is not searching for prereleases.
|
||||
if c.con.pre == "" && v.pre != "" {
|
||||
if !prerelesase {
|
||||
em := fmt.Errorf("%s is a prerelease version and the constraint is only looking for release versions", v)
|
||||
e = append(e, em)
|
||||
prerelesase = true
|
||||
}
|
||||
joy = false
|
||||
|
||||
} else {
|
||||
|
||||
if _, err := c.check(v); err != nil {
|
||||
e = append(e, err)
|
||||
joy = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if joy {
|
||||
return true, []error{}
|
||||
}
|
||||
}
|
||||
|
||||
return false, e
|
||||
}
|
||||
|
||||
func (cs Constraints) String() string {
|
||||
buf := make([]string, len(cs.constraints))
|
||||
var tmp bytes.Buffer
|
||||
|
||||
for k, v := range cs.constraints {
|
||||
tmp.Reset()
|
||||
vlen := len(v)
|
||||
for kk, c := range v {
|
||||
tmp.WriteString(c.string())
|
||||
|
||||
// Space separate the AND conditions
|
||||
if vlen > 1 && kk < vlen-1 {
|
||||
tmp.WriteString(" ")
|
||||
}
|
||||
}
|
||||
buf[k] = tmp.String()
|
||||
}
|
||||
|
||||
return strings.Join(buf, " || ")
|
||||
}
|
||||
|
||||
// UnmarshalText implements the encoding.TextUnmarshaler interface.
|
||||
func (cs *Constraints) UnmarshalText(text []byte) error {
|
||||
temp, err := NewConstraint(string(text))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*cs = *temp
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalText implements the encoding.TextMarshaler interface.
|
||||
func (cs Constraints) MarshalText() ([]byte, error) {
|
||||
return []byte(cs.String()), nil
|
||||
}
|
||||
|
||||
var constraintOps map[string]cfunc
|
||||
var constraintRegex *regexp.Regexp
|
||||
var constraintRangeRegex *regexp.Regexp
|
||||
|
||||
// Used to find individual constraints within a multi-constraint string
|
||||
var findConstraintRegex *regexp.Regexp
|
||||
|
||||
// Used to validate an segment of ANDs is valid
|
||||
var validConstraintRegex *regexp.Regexp
|
||||
|
||||
const cvRegex string = `v?([0-9|x|X|\*]+)(\.[0-9|x|X|\*]+)?(\.[0-9|x|X|\*]+)?` +
|
||||
`(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?` +
|
||||
`(\+([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?`
|
||||
|
||||
func init() {
|
||||
constraintOps = map[string]cfunc{
|
||||
"": constraintTildeOrEqual,
|
||||
"=": constraintTildeOrEqual,
|
||||
"!=": constraintNotEqual,
|
||||
">": constraintGreaterThan,
|
||||
"<": constraintLessThan,
|
||||
">=": constraintGreaterThanEqual,
|
||||
"=>": constraintGreaterThanEqual,
|
||||
"<=": constraintLessThanEqual,
|
||||
"=<": constraintLessThanEqual,
|
||||
"~": constraintTilde,
|
||||
"~>": constraintTilde,
|
||||
"^": constraintCaret,
|
||||
}
|
||||
|
||||
ops := `=||!=|>|<|>=|=>|<=|=<|~|~>|\^`
|
||||
|
||||
constraintRegex = regexp.MustCompile(fmt.Sprintf(
|
||||
`^\s*(%s)\s*(%s)\s*$`,
|
||||
ops,
|
||||
cvRegex))
|
||||
|
||||
constraintRangeRegex = regexp.MustCompile(fmt.Sprintf(
|
||||
`\s*(%s)\s+-\s+(%s)\s*`,
|
||||
cvRegex, cvRegex))
|
||||
|
||||
findConstraintRegex = regexp.MustCompile(fmt.Sprintf(
|
||||
`(%s)\s*(%s)`,
|
||||
ops,
|
||||
cvRegex))
|
||||
|
||||
// The first time a constraint shows up will look slightly different from
|
||||
// future times it shows up due to a leading space or comma in a given
|
||||
// string.
|
||||
validConstraintRegex = regexp.MustCompile(fmt.Sprintf(
|
||||
`^(\s*(%s)\s*(%s)\s*)((?:\s+|,\s*)(%s)\s*(%s)\s*)*$`,
|
||||
ops,
|
||||
cvRegex,
|
||||
ops,
|
||||
cvRegex))
|
||||
}
|
||||
|
||||
// An individual constraint
|
||||
type constraint struct {
|
||||
// The version used in the constraint check. For example, if a constraint
|
||||
// is '<= 2.0.0' the con a version instance representing 2.0.0.
|
||||
con *Version
|
||||
|
||||
// The original parsed version (e.g., 4.x from != 4.x)
|
||||
orig string
|
||||
|
||||
// The original operator for the constraint
|
||||
origfunc string
|
||||
|
||||
// When an x is used as part of the version (e.g., 1.x)
|
||||
minorDirty bool
|
||||
dirty bool
|
||||
patchDirty bool
|
||||
}
|
||||
|
||||
// Check if a version meets the constraint
|
||||
func (c *constraint) check(v *Version) (bool, error) {
|
||||
return constraintOps[c.origfunc](v, c)
|
||||
}
|
||||
|
||||
// String prints an individual constraint into a string
|
||||
func (c *constraint) string() string {
|
||||
return c.origfunc + c.orig
|
||||
}
|
||||
|
||||
type cfunc func(v *Version, c *constraint) (bool, error)
|
||||
|
||||
func parseConstraint(c string) (*constraint, error) {
|
||||
if len(c) > 0 {
|
||||
m := constraintRegex.FindStringSubmatch(c)
|
||||
if m == nil {
|
||||
return nil, fmt.Errorf("improper constraint: %s", c)
|
||||
}
|
||||
|
||||
cs := &constraint{
|
||||
orig: m[2],
|
||||
origfunc: m[1],
|
||||
}
|
||||
|
||||
ver := m[2]
|
||||
minorDirty := false
|
||||
patchDirty := false
|
||||
dirty := false
|
||||
if isX(m[3]) || m[3] == "" {
|
||||
ver = fmt.Sprintf("0.0.0%s", m[6])
|
||||
dirty = true
|
||||
} else if isX(strings.TrimPrefix(m[4], ".")) || m[4] == "" {
|
||||
minorDirty = true
|
||||
dirty = true
|
||||
ver = fmt.Sprintf("%s.0.0%s", m[3], m[6])
|
||||
} else if isX(strings.TrimPrefix(m[5], ".")) || m[5] == "" {
|
||||
dirty = true
|
||||
patchDirty = true
|
||||
ver = fmt.Sprintf("%s%s.0%s", m[3], m[4], m[6])
|
||||
}
|
||||
|
||||
con, err := NewVersion(ver)
|
||||
if err != nil {
|
||||
|
||||
// The constraintRegex should catch any regex parsing errors. So,
|
||||
// we should never get here.
|
||||
return nil, errors.New("constraint Parser Error")
|
||||
}
|
||||
|
||||
cs.con = con
|
||||
cs.minorDirty = minorDirty
|
||||
cs.patchDirty = patchDirty
|
||||
cs.dirty = dirty
|
||||
|
||||
return cs, nil
|
||||
}
|
||||
|
||||
// The rest is the special case where an empty string was passed in which
|
||||
// is equivalent to * or >=0.0.0
|
||||
con, err := StrictNewVersion("0.0.0")
|
||||
if err != nil {
|
||||
|
||||
// The constraintRegex should catch any regex parsing errors. So,
|
||||
// we should never get here.
|
||||
return nil, errors.New("constraint Parser Error")
|
||||
}
|
||||
|
||||
cs := &constraint{
|
||||
con: con,
|
||||
orig: c,
|
||||
origfunc: "",
|
||||
minorDirty: false,
|
||||
patchDirty: false,
|
||||
dirty: true,
|
||||
}
|
||||
return cs, nil
|
||||
}
|
||||
|
||||
// Constraint functions
|
||||
func constraintNotEqual(v *Version, c *constraint) (bool, error) {
|
||||
if c.dirty {
|
||||
|
||||
// If there is a pre-release on the version but the constraint isn't looking
|
||||
// for them assume that pre-releases are not compatible. See issue 21 for
|
||||
// more details.
|
||||
if v.Prerelease() != "" && c.con.Prerelease() == "" {
|
||||
return false, fmt.Errorf("%s is a prerelease version and the constraint is only looking for release versions", v)
|
||||
}
|
||||
|
||||
if c.con.Major() != v.Major() {
|
||||
return true, nil
|
||||
}
|
||||
if c.con.Minor() != v.Minor() && !c.minorDirty {
|
||||
return true, nil
|
||||
} else if c.minorDirty {
|
||||
return false, fmt.Errorf("%s is equal to %s", v, c.orig)
|
||||
} else if c.con.Patch() != v.Patch() && !c.patchDirty {
|
||||
return true, nil
|
||||
} else if c.patchDirty {
|
||||
// Need to handle prereleases if present
|
||||
if v.Prerelease() != "" || c.con.Prerelease() != "" {
|
||||
eq := comparePrerelease(v.Prerelease(), c.con.Prerelease()) != 0
|
||||
if eq {
|
||||
return true, nil
|
||||
}
|
||||
return false, fmt.Errorf("%s is equal to %s", v, c.orig)
|
||||
}
|
||||
return false, fmt.Errorf("%s is equal to %s", v, c.orig)
|
||||
}
|
||||
}
|
||||
|
||||
eq := v.Equal(c.con)
|
||||
if eq {
|
||||
return false, fmt.Errorf("%s is equal to %s", v, c.orig)
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func constraintGreaterThan(v *Version, c *constraint) (bool, error) {
|
||||
|
||||
// If there is a pre-release on the version but the constraint isn't looking
|
||||
// for them assume that pre-releases are not compatible. See issue 21 for
|
||||
// more details.
|
||||
if v.Prerelease() != "" && c.con.Prerelease() == "" {
|
||||
return false, fmt.Errorf("%s is a prerelease version and the constraint is only looking for release versions", v)
|
||||
}
|
||||
|
||||
var eq bool
|
||||
|
||||
if !c.dirty {
|
||||
eq = v.Compare(c.con) == 1
|
||||
if eq {
|
||||
return true, nil
|
||||
}
|
||||
return false, fmt.Errorf("%s is less than or equal to %s", v, c.orig)
|
||||
}
|
||||
|
||||
if v.Major() > c.con.Major() {
|
||||
return true, nil
|
||||
} else if v.Major() < c.con.Major() {
|
||||
return false, fmt.Errorf("%s is less than or equal to %s", v, c.orig)
|
||||
} else if c.minorDirty {
|
||||
// This is a range case such as >11. When the version is something like
|
||||
// 11.1.0 is it not > 11. For that we would need 12 or higher
|
||||
return false, fmt.Errorf("%s is less than or equal to %s", v, c.orig)
|
||||
} else if c.patchDirty {
|
||||
// This is for ranges such as >11.1. A version of 11.1.1 is not greater
|
||||
// which one of 11.2.1 is greater
|
||||
eq = v.Minor() > c.con.Minor()
|
||||
if eq {
|
||||
return true, nil
|
||||
}
|
||||
return false, fmt.Errorf("%s is less than or equal to %s", v, c.orig)
|
||||
}
|
||||
|
||||
// If we have gotten here we are not comparing pre-preleases and can use the
|
||||
// Compare function to accomplish that.
|
||||
eq = v.Compare(c.con) == 1
|
||||
if eq {
|
||||
return true, nil
|
||||
}
|
||||
return false, fmt.Errorf("%s is less than or equal to %s", v, c.orig)
|
||||
}
|
||||
|
||||
func constraintLessThan(v *Version, c *constraint) (bool, error) {
|
||||
// If there is a pre-release on the version but the constraint isn't looking
|
||||
// for them assume that pre-releases are not compatible. See issue 21 for
|
||||
// more details.
|
||||
if v.Prerelease() != "" && c.con.Prerelease() == "" {
|
||||
return false, fmt.Errorf("%s is a prerelease version and the constraint is only looking for release versions", v)
|
||||
}
|
||||
|
||||
eq := v.Compare(c.con) < 0
|
||||
if eq {
|
||||
return true, nil
|
||||
}
|
||||
return false, fmt.Errorf("%s is greater than or equal to %s", v, c.orig)
|
||||
}
|
||||
|
||||
func constraintGreaterThanEqual(v *Version, c *constraint) (bool, error) {
|
||||
|
||||
// If there is a pre-release on the version but the constraint isn't looking
|
||||
// for them assume that pre-releases are not compatible. See issue 21 for
|
||||
// more details.
|
||||
if v.Prerelease() != "" && c.con.Prerelease() == "" {
|
||||
return false, fmt.Errorf("%s is a prerelease version and the constraint is only looking for release versions", v)
|
||||
}
|
||||
|
||||
eq := v.Compare(c.con) >= 0
|
||||
if eq {
|
||||
return true, nil
|
||||
}
|
||||
return false, fmt.Errorf("%s is less than %s", v, c.orig)
|
||||
}
|
||||
|
||||
func constraintLessThanEqual(v *Version, c *constraint) (bool, error) {
|
||||
// If there is a pre-release on the version but the constraint isn't looking
|
||||
// for them assume that pre-releases are not compatible. See issue 21 for
|
||||
// more details.
|
||||
if v.Prerelease() != "" && c.con.Prerelease() == "" {
|
||||
return false, fmt.Errorf("%s is a prerelease version and the constraint is only looking for release versions", v)
|
||||
}
|
||||
|
||||
var eq bool
|
||||
|
||||
if !c.dirty {
|
||||
eq = v.Compare(c.con) <= 0
|
||||
if eq {
|
||||
return true, nil
|
||||
}
|
||||
return false, fmt.Errorf("%s is greater than %s", v, c.orig)
|
||||
}
|
||||
|
||||
if v.Major() > c.con.Major() {
|
||||
return false, fmt.Errorf("%s is greater than %s", v, c.orig)
|
||||
} else if v.Major() == c.con.Major() && v.Minor() > c.con.Minor() && !c.minorDirty {
|
||||
return false, fmt.Errorf("%s is greater than %s", v, c.orig)
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// ~*, ~>* --> >= 0.0.0 (any)
|
||||
// ~2, ~2.x, ~2.x.x, ~>2, ~>2.x ~>2.x.x --> >=2.0.0, <3.0.0
|
||||
// ~2.0, ~2.0.x, ~>2.0, ~>2.0.x --> >=2.0.0, <2.1.0
|
||||
// ~1.2, ~1.2.x, ~>1.2, ~>1.2.x --> >=1.2.0, <1.3.0
|
||||
// ~1.2.3, ~>1.2.3 --> >=1.2.3, <1.3.0
|
||||
// ~1.2.0, ~>1.2.0 --> >=1.2.0, <1.3.0
|
||||
func constraintTilde(v *Version, c *constraint) (bool, error) {
|
||||
// If there is a pre-release on the version but the constraint isn't looking
|
||||
// for them assume that pre-releases are not compatible. See issue 21 for
|
||||
// more details.
|
||||
if v.Prerelease() != "" && c.con.Prerelease() == "" {
|
||||
return false, fmt.Errorf("%s is a prerelease version and the constraint is only looking for release versions", v)
|
||||
}
|
||||
|
||||
if v.LessThan(c.con) {
|
||||
return false, fmt.Errorf("%s is less than %s", v, c.orig)
|
||||
}
|
||||
|
||||
// ~0.0.0 is a special case where all constraints are accepted. It's
|
||||
// equivalent to >= 0.0.0.
|
||||
if c.con.Major() == 0 && c.con.Minor() == 0 && c.con.Patch() == 0 &&
|
||||
!c.minorDirty && !c.patchDirty {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
if v.Major() != c.con.Major() {
|
||||
return false, fmt.Errorf("%s does not have same major version as %s", v, c.orig)
|
||||
}
|
||||
|
||||
if v.Minor() != c.con.Minor() && !c.minorDirty {
|
||||
return false, fmt.Errorf("%s does not have same major and minor version as %s", v, c.orig)
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// When there is a .x (dirty) status it automatically opts in to ~. Otherwise
|
||||
// it's a straight =
|
||||
func constraintTildeOrEqual(v *Version, c *constraint) (bool, error) {
|
||||
// If there is a pre-release on the version but the constraint isn't looking
|
||||
// for them assume that pre-releases are not compatible. See issue 21 for
|
||||
// more details.
|
||||
if v.Prerelease() != "" && c.con.Prerelease() == "" {
|
||||
return false, fmt.Errorf("%s is a prerelease version and the constraint is only looking for release versions", v)
|
||||
}
|
||||
|
||||
if c.dirty {
|
||||
return constraintTilde(v, c)
|
||||
}
|
||||
|
||||
eq := v.Equal(c.con)
|
||||
if eq {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return false, fmt.Errorf("%s is not equal to %s", v, c.orig)
|
||||
}
|
||||
|
||||
// ^* --> (any)
|
||||
// ^1.2.3 --> >=1.2.3 <2.0.0
|
||||
// ^1.2 --> >=1.2.0 <2.0.0
|
||||
// ^1 --> >=1.0.0 <2.0.0
|
||||
// ^0.2.3 --> >=0.2.3 <0.3.0
|
||||
// ^0.2 --> >=0.2.0 <0.3.0
|
||||
// ^0.0.3 --> >=0.0.3 <0.0.4
|
||||
// ^0.0 --> >=0.0.0 <0.1.0
|
||||
// ^0 --> >=0.0.0 <1.0.0
|
||||
func constraintCaret(v *Version, c *constraint) (bool, error) {
|
||||
// If there is a pre-release on the version but the constraint isn't looking
|
||||
// for them assume that pre-releases are not compatible. See issue 21 for
|
||||
// more details.
|
||||
if v.Prerelease() != "" && c.con.Prerelease() == "" {
|
||||
return false, fmt.Errorf("%s is a prerelease version and the constraint is only looking for release versions", v)
|
||||
}
|
||||
|
||||
// This less than handles prereleases
|
||||
if v.LessThan(c.con) {
|
||||
return false, fmt.Errorf("%s is less than %s", v, c.orig)
|
||||
}
|
||||
|
||||
var eq bool
|
||||
|
||||
// ^ when the major > 0 is >=x.y.z < x+1
|
||||
if c.con.Major() > 0 || c.minorDirty {
|
||||
|
||||
// ^ has to be within a major range for > 0. Everything less than was
|
||||
// filtered out with the LessThan call above. This filters out those
|
||||
// that greater but not within the same major range.
|
||||
eq = v.Major() == c.con.Major()
|
||||
if eq {
|
||||
return true, nil
|
||||
}
|
||||
return false, fmt.Errorf("%s does not have same major version as %s", v, c.orig)
|
||||
}
|
||||
|
||||
// ^ when the major is 0 and minor > 0 is >=0.y.z < 0.y+1
|
||||
if c.con.Major() == 0 && v.Major() > 0 {
|
||||
return false, fmt.Errorf("%s does not have same major version as %s", v, c.orig)
|
||||
}
|
||||
// If the con Minor is > 0 it is not dirty
|
||||
if c.con.Minor() > 0 || c.patchDirty {
|
||||
eq = v.Minor() == c.con.Minor()
|
||||
if eq {
|
||||
return true, nil
|
||||
}
|
||||
return false, fmt.Errorf("%s does not have same minor version as %s. Expected minor versions to match when constraint major version is 0", v, c.orig)
|
||||
}
|
||||
// ^ when the minor is 0 and minor > 0 is =0.0.z
|
||||
if c.con.Minor() == 0 && v.Minor() > 0 {
|
||||
return false, fmt.Errorf("%s does not have same minor version as %s", v, c.orig)
|
||||
}
|
||||
|
||||
// At this point the major is 0 and the minor is 0 and not dirty. The patch
|
||||
// is not dirty so we need to check if they are equal. If they are not equal
|
||||
eq = c.con.Patch() == v.Patch()
|
||||
if eq {
|
||||
return true, nil
|
||||
}
|
||||
return false, fmt.Errorf("%s does not equal %s. Expect version and constraint to equal when major and minor versions are 0", v, c.orig)
|
||||
}
|
||||
|
||||
func isX(x string) bool {
|
||||
switch x {
|
||||
case "x", "*", "X":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func rewriteRange(i string) string {
|
||||
m := constraintRangeRegex.FindAllStringSubmatch(i, -1)
|
||||
if m == nil {
|
||||
return i
|
||||
}
|
||||
o := i
|
||||
for _, v := range m {
|
||||
t := fmt.Sprintf(">= %s, <= %s", v[1], v[11])
|
||||
o = strings.Replace(o, v[0], t, 1)
|
||||
}
|
||||
|
||||
return o
|
||||
}
|
||||
184
vendor/github.com/Masterminds/semver/v3/doc.go
generated
vendored
Normal file
184
vendor/github.com/Masterminds/semver/v3/doc.go
generated
vendored
Normal file
@@ -0,0 +1,184 @@
|
||||
/*
|
||||
Package semver provides the ability to work with Semantic Versions (http://semver.org) in Go.
|
||||
|
||||
Specifically it provides the ability to:
|
||||
|
||||
- Parse semantic versions
|
||||
- Sort semantic versions
|
||||
- Check if a semantic version fits within a set of constraints
|
||||
- Optionally work with a `v` prefix
|
||||
|
||||
# Parsing Semantic Versions
|
||||
|
||||
There are two functions that can parse semantic versions. The `StrictNewVersion`
|
||||
function only parses valid version 2 semantic versions as outlined in the
|
||||
specification. The `NewVersion` function attempts to coerce a version into a
|
||||
semantic version and parse it. For example, if there is a leading v or a version
|
||||
listed without all 3 parts (e.g. 1.2) it will attempt to coerce it into a valid
|
||||
semantic version (e.g., 1.2.0). In both cases a `Version` object is returned
|
||||
that can be sorted, compared, and used in constraints.
|
||||
|
||||
When parsing a version an optional error can be returned if there is an issue
|
||||
parsing the version. For example,
|
||||
|
||||
v, err := semver.NewVersion("1.2.3-beta.1+b345")
|
||||
|
||||
The version object has methods to get the parts of the version, compare it to
|
||||
other versions, convert the version back into a string, and get the original
|
||||
string. For more details please see the documentation
|
||||
at https://godoc.org/github.com/Masterminds/semver.
|
||||
|
||||
# Sorting Semantic Versions
|
||||
|
||||
A set of versions can be sorted using the `sort` package from the standard library.
|
||||
For example,
|
||||
|
||||
raw := []string{"1.2.3", "1.0", "1.3", "2", "0.4.2",}
|
||||
vs := make([]*semver.Version, len(raw))
|
||||
for i, r := range raw {
|
||||
v, err := semver.NewVersion(r)
|
||||
if err != nil {
|
||||
t.Errorf("Error parsing version: %s", err)
|
||||
}
|
||||
|
||||
vs[i] = v
|
||||
}
|
||||
|
||||
sort.Sort(semver.Collection(vs))
|
||||
|
||||
# Checking Version Constraints and Comparing Versions
|
||||
|
||||
There are two methods for comparing versions. One uses comparison methods on
|
||||
`Version` instances and the other is using Constraints. There are some important
|
||||
differences to notes between these two methods of comparison.
|
||||
|
||||
1. When two versions are compared using functions such as `Compare`, `LessThan`,
|
||||
and others it will follow the specification and always include prereleases
|
||||
within the comparison. It will provide an answer valid with the comparison
|
||||
spec section at https://semver.org/#spec-item-11
|
||||
2. When constraint checking is used for checks or validation it will follow a
|
||||
different set of rules that are common for ranges with tools like npm/js
|
||||
and Rust/Cargo. This includes considering prereleases to be invalid if the
|
||||
ranges does not include on. If you want to have it include pre-releases a
|
||||
simple solution is to include `-0` in your range.
|
||||
3. Constraint ranges can have some complex rules including the shorthard use of
|
||||
~ and ^. For more details on those see the options below.
|
||||
|
||||
There are differences between the two methods or checking versions because the
|
||||
comparison methods on `Version` follow the specification while comparison ranges
|
||||
are not part of the specification. Different packages and tools have taken it
|
||||
upon themselves to come up with range rules. This has resulted in differences.
|
||||
For example, npm/js and Cargo/Rust follow similar patterns which PHP has a
|
||||
different pattern for ^. The comparison features in this package follow the
|
||||
npm/js and Cargo/Rust lead because applications using it have followed similar
|
||||
patters with their versions.
|
||||
|
||||
Checking a version against version constraints is one of the most featureful
|
||||
parts of the package.
|
||||
|
||||
c, err := semver.NewConstraint(">= 1.2.3")
|
||||
if err != nil {
|
||||
// Handle constraint not being parsable.
|
||||
}
|
||||
|
||||
v, err := semver.NewVersion("1.3")
|
||||
if err != nil {
|
||||
// Handle version not being parsable.
|
||||
}
|
||||
// Check if the version meets the constraints. The a variable will be true.
|
||||
a := c.Check(v)
|
||||
|
||||
# Basic Comparisons
|
||||
|
||||
There are two elements to the comparisons. First, a comparison string is a list
|
||||
of comma or space separated AND comparisons. These are then separated by || (OR)
|
||||
comparisons. For example, `">= 1.2 < 3.0.0 || >= 4.2.3"` is looking for a
|
||||
comparison that's greater than or equal to 1.2 and less than 3.0.0 or is
|
||||
greater than or equal to 4.2.3. This can also be written as
|
||||
`">= 1.2, < 3.0.0 || >= 4.2.3"`
|
||||
|
||||
The basic comparisons are:
|
||||
|
||||
- `=`: equal (aliased to no operator)
|
||||
- `!=`: not equal
|
||||
- `>`: greater than
|
||||
- `<`: less than
|
||||
- `>=`: greater than or equal to
|
||||
- `<=`: less than or equal to
|
||||
|
||||
# Hyphen Range Comparisons
|
||||
|
||||
There are multiple methods to handle ranges and the first is hyphens ranges.
|
||||
These look like:
|
||||
|
||||
- `1.2 - 1.4.5` which is equivalent to `>= 1.2, <= 1.4.5`
|
||||
- `2.3.4 - 4.5` which is equivalent to `>= 2.3.4 <= 4.5`
|
||||
|
||||
# Wildcards In Comparisons
|
||||
|
||||
The `x`, `X`, and `*` characters can be used as a wildcard character. This works
|
||||
for all comparison operators. When used on the `=` operator it falls
|
||||
back to the tilde operation. For example,
|
||||
|
||||
- `1.2.x` is equivalent to `>= 1.2.0 < 1.3.0`
|
||||
- `>= 1.2.x` is equivalent to `>= 1.2.0`
|
||||
- `<= 2.x` is equivalent to `<= 3`
|
||||
- `*` is equivalent to `>= 0.0.0`
|
||||
|
||||
Tilde Range Comparisons (Patch)
|
||||
|
||||
The tilde (`~`) comparison operator is for patch level ranges when a minor
|
||||
version is specified and major level changes when the minor number is missing.
|
||||
For example,
|
||||
|
||||
- `~1.2.3` is equivalent to `>= 1.2.3 < 1.3.0`
|
||||
- `~1` is equivalent to `>= 1, < 2`
|
||||
- `~2.3` is equivalent to `>= 2.3 < 2.4`
|
||||
- `~1.2.x` is equivalent to `>= 1.2.0 < 1.3.0`
|
||||
- `~1.x` is equivalent to `>= 1 < 2`
|
||||
|
||||
Caret Range Comparisons (Major)
|
||||
|
||||
The caret (`^`) comparison operator is for major level changes once a stable
|
||||
(1.0.0) release has occurred. Prior to a 1.0.0 release the minor versions acts
|
||||
as the API stability level. This is useful when comparisons of API versions as a
|
||||
major change is API breaking. For example,
|
||||
|
||||
- `^1.2.3` is equivalent to `>= 1.2.3, < 2.0.0`
|
||||
- `^1.2.x` is equivalent to `>= 1.2.0, < 2.0.0`
|
||||
- `^2.3` is equivalent to `>= 2.3, < 3`
|
||||
- `^2.x` is equivalent to `>= 2.0.0, < 3`
|
||||
- `^0.2.3` is equivalent to `>=0.2.3 <0.3.0`
|
||||
- `^0.2` is equivalent to `>=0.2.0 <0.3.0`
|
||||
- `^0.0.3` is equivalent to `>=0.0.3 <0.0.4`
|
||||
- `^0.0` is equivalent to `>=0.0.0 <0.1.0`
|
||||
- `^0` is equivalent to `>=0.0.0 <1.0.0`
|
||||
|
||||
# Validation
|
||||
|
||||
In addition to testing a version against a constraint, a version can be validated
|
||||
against a constraint. When validation fails a slice of errors containing why a
|
||||
version didn't meet the constraint is returned. For example,
|
||||
|
||||
c, err := semver.NewConstraint("<= 1.2.3, >= 1.4")
|
||||
if err != nil {
|
||||
// Handle constraint not being parseable.
|
||||
}
|
||||
|
||||
v, _ := semver.NewVersion("1.3")
|
||||
if err != nil {
|
||||
// Handle version not being parseable.
|
||||
}
|
||||
|
||||
// Validate a version against a constraint.
|
||||
a, msgs := c.Validate(v)
|
||||
// a is false
|
||||
for _, m := range msgs {
|
||||
fmt.Println(m)
|
||||
|
||||
// Loops over the errors which would read
|
||||
// "1.3 is greater than 1.2.3"
|
||||
// "1.3 is less than 1.4"
|
||||
}
|
||||
*/
|
||||
package semver
|
||||
22
vendor/github.com/Masterminds/semver/v3/fuzz.go
generated
vendored
Normal file
22
vendor/github.com/Masterminds/semver/v3/fuzz.go
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
// +build gofuzz
|
||||
|
||||
package semver
|
||||
|
||||
func Fuzz(data []byte) int {
|
||||
d := string(data)
|
||||
|
||||
// Test NewVersion
|
||||
_, _ = NewVersion(d)
|
||||
|
||||
// Test StrictNewVersion
|
||||
_, _ = StrictNewVersion(d)
|
||||
|
||||
// Test NewConstraint
|
||||
_, _ = NewConstraint(d)
|
||||
|
||||
// The return value should be 0 normally, 1 if the priority in future tests
|
||||
// should be increased, and -1 if future tests should skip passing in that
|
||||
// data. We do not have a reason to change priority so 0 is always returned.
|
||||
// There are example tests that do this.
|
||||
return 0
|
||||
}
|
||||
639
vendor/github.com/Masterminds/semver/v3/version.go
generated
vendored
Normal file
639
vendor/github.com/Masterminds/semver/v3/version.go
generated
vendored
Normal file
@@ -0,0 +1,639 @@
|
||||
package semver
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// The compiled version of the regex created at init() is cached here so it
|
||||
// only needs to be created once.
|
||||
var versionRegex *regexp.Regexp
|
||||
|
||||
var (
|
||||
// ErrInvalidSemVer is returned a version is found to be invalid when
|
||||
// being parsed.
|
||||
ErrInvalidSemVer = errors.New("Invalid Semantic Version")
|
||||
|
||||
// ErrEmptyString is returned when an empty string is passed in for parsing.
|
||||
ErrEmptyString = errors.New("Version string empty")
|
||||
|
||||
// ErrInvalidCharacters is returned when invalid characters are found as
|
||||
// part of a version
|
||||
ErrInvalidCharacters = errors.New("Invalid characters in version")
|
||||
|
||||
// ErrSegmentStartsZero is returned when a version segment starts with 0.
|
||||
// This is invalid in SemVer.
|
||||
ErrSegmentStartsZero = errors.New("Version segment starts with 0")
|
||||
|
||||
// ErrInvalidMetadata is returned when the metadata is an invalid format
|
||||
ErrInvalidMetadata = errors.New("Invalid Metadata string")
|
||||
|
||||
// ErrInvalidPrerelease is returned when the pre-release is an invalid format
|
||||
ErrInvalidPrerelease = errors.New("Invalid Prerelease string")
|
||||
)
|
||||
|
||||
// semVerRegex is the regular expression used to parse a semantic version.
|
||||
const semVerRegex string = `v?([0-9]+)(\.[0-9]+)?(\.[0-9]+)?` +
|
||||
`(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?` +
|
||||
`(\+([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?`
|
||||
|
||||
// Version represents a single semantic version.
|
||||
type Version struct {
|
||||
major, minor, patch uint64
|
||||
pre string
|
||||
metadata string
|
||||
original string
|
||||
}
|
||||
|
||||
func init() {
|
||||
versionRegex = regexp.MustCompile("^" + semVerRegex + "$")
|
||||
}
|
||||
|
||||
const (
|
||||
num string = "0123456789"
|
||||
allowed string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-" + num
|
||||
)
|
||||
|
||||
// StrictNewVersion parses a given version and returns an instance of Version or
|
||||
// an error if unable to parse the version. Only parses valid semantic versions.
|
||||
// Performs checking that can find errors within the version.
|
||||
// If you want to coerce a version such as 1 or 1.2 and parse it as the 1.x
|
||||
// releases of semver did, use the NewVersion() function.
|
||||
func StrictNewVersion(v string) (*Version, error) {
|
||||
// Parsing here does not use RegEx in order to increase performance and reduce
|
||||
// allocations.
|
||||
|
||||
if len(v) == 0 {
|
||||
return nil, ErrEmptyString
|
||||
}
|
||||
|
||||
// Split the parts into [0]major, [1]minor, and [2]patch,prerelease,build
|
||||
parts := strings.SplitN(v, ".", 3)
|
||||
if len(parts) != 3 {
|
||||
return nil, ErrInvalidSemVer
|
||||
}
|
||||
|
||||
sv := &Version{
|
||||
original: v,
|
||||
}
|
||||
|
||||
// check for prerelease or build metadata
|
||||
var extra []string
|
||||
if strings.ContainsAny(parts[2], "-+") {
|
||||
// Start with the build metadata first as it needs to be on the right
|
||||
extra = strings.SplitN(parts[2], "+", 2)
|
||||
if len(extra) > 1 {
|
||||
// build metadata found
|
||||
sv.metadata = extra[1]
|
||||
parts[2] = extra[0]
|
||||
}
|
||||
|
||||
extra = strings.SplitN(parts[2], "-", 2)
|
||||
if len(extra) > 1 {
|
||||
// prerelease found
|
||||
sv.pre = extra[1]
|
||||
parts[2] = extra[0]
|
||||
}
|
||||
}
|
||||
|
||||
// Validate the number segments are valid. This includes only having positive
|
||||
// numbers and no leading 0's.
|
||||
for _, p := range parts {
|
||||
if !containsOnly(p, num) {
|
||||
return nil, ErrInvalidCharacters
|
||||
}
|
||||
|
||||
if len(p) > 1 && p[0] == '0' {
|
||||
return nil, ErrSegmentStartsZero
|
||||
}
|
||||
}
|
||||
|
||||
// Extract the major, minor, and patch elements onto the returned Version
|
||||
var err error
|
||||
sv.major, err = strconv.ParseUint(parts[0], 10, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sv.minor, err = strconv.ParseUint(parts[1], 10, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sv.patch, err = strconv.ParseUint(parts[2], 10, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// No prerelease or build metadata found so returning now as a fastpath.
|
||||
if sv.pre == "" && sv.metadata == "" {
|
||||
return sv, nil
|
||||
}
|
||||
|
||||
if sv.pre != "" {
|
||||
if err = validatePrerelease(sv.pre); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if sv.metadata != "" {
|
||||
if err = validateMetadata(sv.metadata); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return sv, nil
|
||||
}
|
||||
|
||||
// NewVersion parses a given version and returns an instance of Version or
|
||||
// an error if unable to parse the version. If the version is SemVer-ish it
|
||||
// attempts to convert it to SemVer. If you want to validate it was a strict
|
||||
// semantic version at parse time see StrictNewVersion().
|
||||
func NewVersion(v string) (*Version, error) {
|
||||
m := versionRegex.FindStringSubmatch(v)
|
||||
if m == nil {
|
||||
return nil, ErrInvalidSemVer
|
||||
}
|
||||
|
||||
sv := &Version{
|
||||
metadata: m[8],
|
||||
pre: m[5],
|
||||
original: v,
|
||||
}
|
||||
|
||||
var err error
|
||||
sv.major, err = strconv.ParseUint(m[1], 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error parsing version segment: %s", err)
|
||||
}
|
||||
|
||||
if m[2] != "" {
|
||||
sv.minor, err = strconv.ParseUint(strings.TrimPrefix(m[2], "."), 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error parsing version segment: %s", err)
|
||||
}
|
||||
} else {
|
||||
sv.minor = 0
|
||||
}
|
||||
|
||||
if m[3] != "" {
|
||||
sv.patch, err = strconv.ParseUint(strings.TrimPrefix(m[3], "."), 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error parsing version segment: %s", err)
|
||||
}
|
||||
} else {
|
||||
sv.patch = 0
|
||||
}
|
||||
|
||||
// Perform some basic due diligence on the extra parts to ensure they are
|
||||
// valid.
|
||||
|
||||
if sv.pre != "" {
|
||||
if err = validatePrerelease(sv.pre); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if sv.metadata != "" {
|
||||
if err = validateMetadata(sv.metadata); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return sv, nil
|
||||
}
|
||||
|
||||
// New creates a new instance of Version with each of the parts passed in as
|
||||
// arguments instead of parsing a version string.
|
||||
func New(major, minor, patch uint64, pre, metadata string) *Version {
|
||||
v := Version{
|
||||
major: major,
|
||||
minor: minor,
|
||||
patch: patch,
|
||||
pre: pre,
|
||||
metadata: metadata,
|
||||
original: "",
|
||||
}
|
||||
|
||||
v.original = v.String()
|
||||
|
||||
return &v
|
||||
}
|
||||
|
||||
// MustParse parses a given version and panics on error.
|
||||
func MustParse(v string) *Version {
|
||||
sv, err := NewVersion(v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return sv
|
||||
}
|
||||
|
||||
// String converts a Version object to a string.
|
||||
// Note, if the original version contained a leading v this version will not.
|
||||
// See the Original() method to retrieve the original value. Semantic Versions
|
||||
// don't contain a leading v per the spec. Instead it's optional on
|
||||
// implementation.
|
||||
func (v Version) String() string {
|
||||
var buf bytes.Buffer
|
||||
|
||||
fmt.Fprintf(&buf, "%d.%d.%d", v.major, v.minor, v.patch)
|
||||
if v.pre != "" {
|
||||
fmt.Fprintf(&buf, "-%s", v.pre)
|
||||
}
|
||||
if v.metadata != "" {
|
||||
fmt.Fprintf(&buf, "+%s", v.metadata)
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// Original returns the original value passed in to be parsed.
|
||||
func (v *Version) Original() string {
|
||||
return v.original
|
||||
}
|
||||
|
||||
// Major returns the major version.
|
||||
func (v Version) Major() uint64 {
|
||||
return v.major
|
||||
}
|
||||
|
||||
// Minor returns the minor version.
|
||||
func (v Version) Minor() uint64 {
|
||||
return v.minor
|
||||
}
|
||||
|
||||
// Patch returns the patch version.
|
||||
func (v Version) Patch() uint64 {
|
||||
return v.patch
|
||||
}
|
||||
|
||||
// Prerelease returns the pre-release version.
|
||||
func (v Version) Prerelease() string {
|
||||
return v.pre
|
||||
}
|
||||
|
||||
// Metadata returns the metadata on the version.
|
||||
func (v Version) Metadata() string {
|
||||
return v.metadata
|
||||
}
|
||||
|
||||
// originalVPrefix returns the original 'v' prefix if any.
|
||||
func (v Version) originalVPrefix() string {
|
||||
// Note, only lowercase v is supported as a prefix by the parser.
|
||||
if v.original != "" && v.original[:1] == "v" {
|
||||
return v.original[:1]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// IncPatch produces the next patch version.
|
||||
// If the current version does not have prerelease/metadata information,
|
||||
// it unsets metadata and prerelease values, increments patch number.
|
||||
// If the current version has any of prerelease or metadata information,
|
||||
// it unsets both values and keeps current patch value
|
||||
func (v Version) IncPatch() Version {
|
||||
vNext := v
|
||||
// according to http://semver.org/#spec-item-9
|
||||
// Pre-release versions have a lower precedence than the associated normal version.
|
||||
// according to http://semver.org/#spec-item-10
|
||||
// Build metadata SHOULD be ignored when determining version precedence.
|
||||
if v.pre != "" {
|
||||
vNext.metadata = ""
|
||||
vNext.pre = ""
|
||||
} else {
|
||||
vNext.metadata = ""
|
||||
vNext.pre = ""
|
||||
vNext.patch = v.patch + 1
|
||||
}
|
||||
vNext.original = v.originalVPrefix() + "" + vNext.String()
|
||||
return vNext
|
||||
}
|
||||
|
||||
// IncMinor produces the next minor version.
|
||||
// Sets patch to 0.
|
||||
// Increments minor number.
|
||||
// Unsets metadata.
|
||||
// Unsets prerelease status.
|
||||
func (v Version) IncMinor() Version {
|
||||
vNext := v
|
||||
vNext.metadata = ""
|
||||
vNext.pre = ""
|
||||
vNext.patch = 0
|
||||
vNext.minor = v.minor + 1
|
||||
vNext.original = v.originalVPrefix() + "" + vNext.String()
|
||||
return vNext
|
||||
}
|
||||
|
||||
// IncMajor produces the next major version.
|
||||
// Sets patch to 0.
|
||||
// Sets minor to 0.
|
||||
// Increments major number.
|
||||
// Unsets metadata.
|
||||
// Unsets prerelease status.
|
||||
func (v Version) IncMajor() Version {
|
||||
vNext := v
|
||||
vNext.metadata = ""
|
||||
vNext.pre = ""
|
||||
vNext.patch = 0
|
||||
vNext.minor = 0
|
||||
vNext.major = v.major + 1
|
||||
vNext.original = v.originalVPrefix() + "" + vNext.String()
|
||||
return vNext
|
||||
}
|
||||
|
||||
// SetPrerelease defines the prerelease value.
|
||||
// Value must not include the required 'hyphen' prefix.
|
||||
func (v Version) SetPrerelease(prerelease string) (Version, error) {
|
||||
vNext := v
|
||||
if len(prerelease) > 0 {
|
||||
if err := validatePrerelease(prerelease); err != nil {
|
||||
return vNext, err
|
||||
}
|
||||
}
|
||||
vNext.pre = prerelease
|
||||
vNext.original = v.originalVPrefix() + "" + vNext.String()
|
||||
return vNext, nil
|
||||
}
|
||||
|
||||
// SetMetadata defines metadata value.
|
||||
// Value must not include the required 'plus' prefix.
|
||||
func (v Version) SetMetadata(metadata string) (Version, error) {
|
||||
vNext := v
|
||||
if len(metadata) > 0 {
|
||||
if err := validateMetadata(metadata); err != nil {
|
||||
return vNext, err
|
||||
}
|
||||
}
|
||||
vNext.metadata = metadata
|
||||
vNext.original = v.originalVPrefix() + "" + vNext.String()
|
||||
return vNext, nil
|
||||
}
|
||||
|
||||
// LessThan tests if one version is less than another one.
|
||||
func (v *Version) LessThan(o *Version) bool {
|
||||
return v.Compare(o) < 0
|
||||
}
|
||||
|
||||
// GreaterThan tests if one version is greater than another one.
|
||||
func (v *Version) GreaterThan(o *Version) bool {
|
||||
return v.Compare(o) > 0
|
||||
}
|
||||
|
||||
// Equal tests if two versions are equal to each other.
|
||||
// Note, versions can be equal with different metadata since metadata
|
||||
// is not considered part of the comparable version.
|
||||
func (v *Version) Equal(o *Version) bool {
|
||||
return v.Compare(o) == 0
|
||||
}
|
||||
|
||||
// Compare compares this version to another one. It returns -1, 0, or 1 if
|
||||
// the version smaller, equal, or larger than the other version.
|
||||
//
|
||||
// Versions are compared by X.Y.Z. Build metadata is ignored. Prerelease is
|
||||
// lower than the version without a prerelease. Compare always takes into account
|
||||
// prereleases. If you want to work with ranges using typical range syntaxes that
|
||||
// skip prereleases if the range is not looking for them use constraints.
|
||||
func (v *Version) Compare(o *Version) int {
|
||||
// Compare the major, minor, and patch version for differences. If a
|
||||
// difference is found return the comparison.
|
||||
if d := compareSegment(v.Major(), o.Major()); d != 0 {
|
||||
return d
|
||||
}
|
||||
if d := compareSegment(v.Minor(), o.Minor()); d != 0 {
|
||||
return d
|
||||
}
|
||||
if d := compareSegment(v.Patch(), o.Patch()); d != 0 {
|
||||
return d
|
||||
}
|
||||
|
||||
// At this point the major, minor, and patch versions are the same.
|
||||
ps := v.pre
|
||||
po := o.Prerelease()
|
||||
|
||||
if ps == "" && po == "" {
|
||||
return 0
|
||||
}
|
||||
if ps == "" {
|
||||
return 1
|
||||
}
|
||||
if po == "" {
|
||||
return -1
|
||||
}
|
||||
|
||||
return comparePrerelease(ps, po)
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements JSON.Unmarshaler interface.
|
||||
func (v *Version) UnmarshalJSON(b []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(b, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
temp, err := NewVersion(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v.major = temp.major
|
||||
v.minor = temp.minor
|
||||
v.patch = temp.patch
|
||||
v.pre = temp.pre
|
||||
v.metadata = temp.metadata
|
||||
v.original = temp.original
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON implements JSON.Marshaler interface.
|
||||
func (v Version) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(v.String())
|
||||
}
|
||||
|
||||
// UnmarshalText implements the encoding.TextUnmarshaler interface.
|
||||
func (v *Version) UnmarshalText(text []byte) error {
|
||||
temp, err := NewVersion(string(text))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*v = *temp
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalText implements the encoding.TextMarshaler interface.
|
||||
func (v Version) MarshalText() ([]byte, error) {
|
||||
return []byte(v.String()), nil
|
||||
}
|
||||
|
||||
// Scan implements the SQL.Scanner interface.
|
||||
func (v *Version) Scan(value interface{}) error {
|
||||
var s string
|
||||
s, _ = value.(string)
|
||||
temp, err := NewVersion(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v.major = temp.major
|
||||
v.minor = temp.minor
|
||||
v.patch = temp.patch
|
||||
v.pre = temp.pre
|
||||
v.metadata = temp.metadata
|
||||
v.original = temp.original
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value implements the Driver.Valuer interface.
|
||||
func (v Version) Value() (driver.Value, error) {
|
||||
return v.String(), nil
|
||||
}
|
||||
|
||||
func compareSegment(v, o uint64) int {
|
||||
if v < o {
|
||||
return -1
|
||||
}
|
||||
if v > o {
|
||||
return 1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func comparePrerelease(v, o string) int {
|
||||
// split the prelease versions by their part. The separator, per the spec,
|
||||
// is a .
|
||||
sparts := strings.Split(v, ".")
|
||||
oparts := strings.Split(o, ".")
|
||||
|
||||
// Find the longer length of the parts to know how many loop iterations to
|
||||
// go through.
|
||||
slen := len(sparts)
|
||||
olen := len(oparts)
|
||||
|
||||
l := slen
|
||||
if olen > slen {
|
||||
l = olen
|
||||
}
|
||||
|
||||
// Iterate over each part of the prereleases to compare the differences.
|
||||
for i := 0; i < l; i++ {
|
||||
// Since the lentgh of the parts can be different we need to create
|
||||
// a placeholder. This is to avoid out of bounds issues.
|
||||
stemp := ""
|
||||
if i < slen {
|
||||
stemp = sparts[i]
|
||||
}
|
||||
|
||||
otemp := ""
|
||||
if i < olen {
|
||||
otemp = oparts[i]
|
||||
}
|
||||
|
||||
d := comparePrePart(stemp, otemp)
|
||||
if d != 0 {
|
||||
return d
|
||||
}
|
||||
}
|
||||
|
||||
// Reaching here means two versions are of equal value but have different
|
||||
// metadata (the part following a +). They are not identical in string form
|
||||
// but the version comparison finds them to be equal.
|
||||
return 0
|
||||
}
|
||||
|
||||
func comparePrePart(s, o string) int {
|
||||
// Fastpath if they are equal
|
||||
if s == o {
|
||||
return 0
|
||||
}
|
||||
|
||||
// When s or o are empty we can use the other in an attempt to determine
|
||||
// the response.
|
||||
if s == "" {
|
||||
if o != "" {
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
if o == "" {
|
||||
if s != "" {
|
||||
return 1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// When comparing strings "99" is greater than "103". To handle
|
||||
// cases like this we need to detect numbers and compare them. According
|
||||
// to the semver spec, numbers are always positive. If there is a - at the
|
||||
// start like -99 this is to be evaluated as an alphanum. numbers always
|
||||
// have precedence over alphanum. Parsing as Uints because negative numbers
|
||||
// are ignored.
|
||||
|
||||
oi, n1 := strconv.ParseUint(o, 10, 64)
|
||||
si, n2 := strconv.ParseUint(s, 10, 64)
|
||||
|
||||
// The case where both are strings compare the strings
|
||||
if n1 != nil && n2 != nil {
|
||||
if s > o {
|
||||
return 1
|
||||
}
|
||||
return -1
|
||||
} else if n1 != nil {
|
||||
// o is a string and s is a number
|
||||
return -1
|
||||
} else if n2 != nil {
|
||||
// s is a string and o is a number
|
||||
return 1
|
||||
}
|
||||
// Both are numbers
|
||||
if si > oi {
|
||||
return 1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// Like strings.ContainsAny but does an only instead of any.
|
||||
func containsOnly(s string, comp string) bool {
|
||||
return strings.IndexFunc(s, func(r rune) bool {
|
||||
return !strings.ContainsRune(comp, r)
|
||||
}) == -1
|
||||
}
|
||||
|
||||
// From the spec, "Identifiers MUST comprise only
|
||||
// ASCII alphanumerics and hyphen [0-9A-Za-z-]. Identifiers MUST NOT be empty.
|
||||
// Numeric identifiers MUST NOT include leading zeroes.". These segments can
|
||||
// be dot separated.
|
||||
func validatePrerelease(p string) error {
|
||||
eparts := strings.Split(p, ".")
|
||||
for _, p := range eparts {
|
||||
if containsOnly(p, num) {
|
||||
if len(p) > 1 && p[0] == '0' {
|
||||
return ErrSegmentStartsZero
|
||||
}
|
||||
} else if !containsOnly(p, allowed) {
|
||||
return ErrInvalidPrerelease
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// From the spec, "Build metadata MAY be denoted by
|
||||
// appending a plus sign and a series of dot separated identifiers immediately
|
||||
// following the patch or pre-release version. Identifiers MUST comprise only
|
||||
// ASCII alphanumerics and hyphen [0-9A-Za-z-]. Identifiers MUST NOT be empty."
|
||||
func validateMetadata(m string) error {
|
||||
eparts := strings.Split(m, ".")
|
||||
for _, p := range eparts {
|
||||
if !containsOnly(p, allowed) {
|
||||
return ErrInvalidMetadata
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
2
vendor/github.com/Masterminds/sprig/v3/.gitignore
generated
vendored
Normal file
2
vendor/github.com/Masterminds/sprig/v3/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
vendor/
|
||||
/.glide
|
||||
383
vendor/github.com/Masterminds/sprig/v3/CHANGELOG.md
generated
vendored
Normal file
383
vendor/github.com/Masterminds/sprig/v3/CHANGELOG.md
generated
vendored
Normal file
@@ -0,0 +1,383 @@
|
||||
# Changelog
|
||||
|
||||
## Release 3.2.3 (2022-11-29)
|
||||
|
||||
### Changed
|
||||
|
||||
- Updated docs (thanks @book987 @aJetHorn @neelayu @pellizzetti @apricote @SaigyoujiYuyuko233 @AlekSi)
|
||||
- #348: Updated huandu/xstrings which fixed a snake case bug (thanks @yxxhero)
|
||||
- #353: Updated masterminds/semver which included bug fixes
|
||||
- #354: Updated golang.org/x/crypto which included bug fixes
|
||||
|
||||
## Release 3.2.2 (2021-02-04)
|
||||
|
||||
This is a re-release of 3.2.1 to satisfy something with the Go module system.
|
||||
|
||||
## Release 3.2.1 (2021-02-04)
|
||||
|
||||
### Changed
|
||||
|
||||
- Upgraded `Masterminds/goutils` to `v1.1.1`. see the [Security Advisory](https://github.com/Masterminds/goutils/security/advisories/GHSA-xg2h-wx96-xgxr)
|
||||
|
||||
## Release 3.2.0 (2020-12-14)
|
||||
|
||||
### Added
|
||||
|
||||
- #211: Added randInt function (thanks @kochurovro)
|
||||
- #223: Added fromJson and mustFromJson functions (thanks @mholt)
|
||||
- #242: Added a bcrypt function (thanks @robbiet480)
|
||||
- #253: Added randBytes function (thanks @MikaelSmith)
|
||||
- #254: Added dig function for dicts (thanks @nyarly)
|
||||
- #257: Added regexQuoteMeta for quoting regex metadata (thanks @rheaton)
|
||||
- #261: Added filepath functions osBase, osDir, osExt, osClean, osIsAbs (thanks @zugl)
|
||||
- #268: Added and and all functions for testing conditions (thanks @phuslu)
|
||||
- #181: Added float64 arithmetic addf, add1f, subf, divf, mulf, maxf, and minf
|
||||
(thanks @andrewmostello)
|
||||
- #265: Added chunk function to split array into smaller arrays (thanks @karelbilek)
|
||||
- #270: Extend certificate functions to handle non-RSA keys + add support for
|
||||
ed25519 keys (thanks @misberner)
|
||||
|
||||
### Changed
|
||||
|
||||
- Removed testing and support for Go 1.12. ed25519 support requires Go 1.13 or newer
|
||||
- Using semver 3.1.1 and mergo 0.3.11
|
||||
|
||||
### Fixed
|
||||
|
||||
- #249: Fix htmlDateInZone example (thanks @spawnia)
|
||||
|
||||
NOTE: The dependency github.com/imdario/mergo reverted the breaking change in
|
||||
0.3.9 via 0.3.10 release.
|
||||
|
||||
## Release 3.1.0 (2020-04-16)
|
||||
|
||||
NOTE: The dependency github.com/imdario/mergo made a behavior change in 0.3.9
|
||||
that impacts sprig functionality. Do not use sprig with a version newer than 0.3.8.
|
||||
|
||||
### Added
|
||||
|
||||
- #225: Added support for generating htpasswd hash (thanks @rustycl0ck)
|
||||
- #224: Added duration filter (thanks @frebib)
|
||||
- #205: Added `seq` function (thanks @thadc23)
|
||||
|
||||
### Changed
|
||||
|
||||
- #203: Unlambda functions with correct signature (thanks @muesli)
|
||||
- #236: Updated the license formatting for GitHub display purposes
|
||||
- #238: Updated package dependency versions. Note, mergo not updated to 0.3.9
|
||||
as it causes a breaking change for sprig. That issue is tracked at
|
||||
https://github.com/imdario/mergo/issues/139
|
||||
|
||||
### Fixed
|
||||
|
||||
- #229: Fix `seq` example in docs (thanks @kalmant)
|
||||
|
||||
## Release 3.0.2 (2019-12-13)
|
||||
|
||||
### Fixed
|
||||
|
||||
- #220: Updating to semver v3.0.3 to fix issue with <= ranges
|
||||
- #218: fix typo elyptical->elliptic in ecdsa key description (thanks @laverya)
|
||||
|
||||
## Release 3.0.1 (2019-12-08)
|
||||
|
||||
### Fixed
|
||||
|
||||
- #212: Updated semver fixing broken constraint checking with ^0.0
|
||||
|
||||
## Release 3.0.0 (2019-10-02)
|
||||
|
||||
### Added
|
||||
|
||||
- #187: Added durationRound function (thanks @yjp20)
|
||||
- #189: Added numerous template functions that return errors rather than panic (thanks @nrvnrvn)
|
||||
- #193: Added toRawJson support (thanks @Dean-Coakley)
|
||||
- #197: Added get support to dicts (thanks @Dean-Coakley)
|
||||
|
||||
### Changed
|
||||
|
||||
- #186: Moving dependency management to Go modules
|
||||
- #186: Updated semver to v3. This has changes in the way ^ is handled
|
||||
- #194: Updated documentation on merging and how it copies. Added example using deepCopy
|
||||
- #196: trunc now supports negative values (thanks @Dean-Coakley)
|
||||
|
||||
## Release 2.22.0 (2019-10-02)
|
||||
|
||||
### Added
|
||||
|
||||
- #173: Added getHostByName function to resolve dns names to ips (thanks @fcgravalos)
|
||||
- #195: Added deepCopy function for use with dicts
|
||||
|
||||
### Changed
|
||||
|
||||
- Updated merge and mergeOverwrite documentation to explain copying and how to
|
||||
use deepCopy with it
|
||||
|
||||
## Release 2.21.0 (2019-09-18)
|
||||
|
||||
### Added
|
||||
|
||||
- #122: Added encryptAES/decryptAES functions (thanks @n0madic)
|
||||
- #128: Added toDecimal support (thanks @Dean-Coakley)
|
||||
- #169: Added list contcat (thanks @astorath)
|
||||
- #174: Added deepEqual function (thanks @bonifaido)
|
||||
- #170: Added url parse and join functions (thanks @astorath)
|
||||
|
||||
### Changed
|
||||
|
||||
- #171: Updated glide config for Google UUID to v1 and to add ranges to semver and testify
|
||||
|
||||
### Fixed
|
||||
|
||||
- #172: Fix semver wildcard example (thanks @piepmatz)
|
||||
- #175: Fix dateInZone doc example (thanks @s3than)
|
||||
|
||||
## Release 2.20.0 (2019-06-18)
|
||||
|
||||
### Added
|
||||
|
||||
- #164: Adding function to get unix epoch for a time (@mattfarina)
|
||||
- #166: Adding tests for date_in_zone (@mattfarina)
|
||||
|
||||
### Changed
|
||||
|
||||
- #144: Fix function comments based on best practices from Effective Go (@CodeLingoTeam)
|
||||
- #150: Handles pointer type for time.Time in "htmlDate" (@mapreal19)
|
||||
- #161, #157, #160, #153, #158, #156, #155, #159, #152 documentation updates (@badeadan)
|
||||
|
||||
### Fixed
|
||||
|
||||
## Release 2.19.0 (2019-03-02)
|
||||
|
||||
IMPORTANT: This release reverts a change from 2.18.0
|
||||
|
||||
In the previous release (2.18), we prematurely merged a partial change to the crypto functions that led to creating two sets of crypto functions (I blame @technosophos -- since that's me). This release rolls back that change, and does what was originally intended: It alters the existing crypto functions to use secure random.
|
||||
|
||||
We debated whether this classifies as a change worthy of major revision, but given the proximity to the last release, we have decided that treating 2.18 as a faulty release is the correct course of action. We apologize for any inconvenience.
|
||||
|
||||
### Changed
|
||||
|
||||
- Fix substr panic 35fb796 (Alexey igrychev)
|
||||
- Remove extra period 1eb7729 (Matthew Lorimor)
|
||||
- Make random string functions use crypto by default 6ceff26 (Matthew Lorimor)
|
||||
- README edits/fixes/suggestions 08fe136 (Lauri Apple)
|
||||
|
||||
|
||||
## Release 2.18.0 (2019-02-12)
|
||||
|
||||
### Added
|
||||
|
||||
- Added mergeOverwrite function
|
||||
- cryptographic functions that use secure random (see fe1de12)
|
||||
|
||||
### Changed
|
||||
|
||||
- Improve documentation of regexMatch function, resolves #139 90b89ce (Jan Tagscherer)
|
||||
- Handle has for nil list 9c10885 (Daniel Cohen)
|
||||
- Document behaviour of mergeOverwrite fe0dbe9 (Lukas Rieder)
|
||||
- doc: adds missing documentation. 4b871e6 (Fernandez Ludovic)
|
||||
- Replace outdated goutils imports 01893d2 (Matthew Lorimor)
|
||||
- Surface crypto secure random strings from goutils fe1de12 (Matthew Lorimor)
|
||||
- Handle untyped nil values as paramters to string functions 2b2ec8f (Morten Torkildsen)
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fix dict merge issue and provide mergeOverwrite .dst .src1 to overwrite from src -> dst 4c59c12 (Lukas Rieder)
|
||||
- Fix substr var names and comments d581f80 (Dean Coakley)
|
||||
- Fix substr documentation 2737203 (Dean Coakley)
|
||||
|
||||
## Release 2.17.1 (2019-01-03)
|
||||
|
||||
### Fixed
|
||||
|
||||
The 2.17.0 release did not have a version pinned for xstrings, which caused compilation failures when xstrings < 1.2 was used. This adds the correct version string to glide.yaml.
|
||||
|
||||
## Release 2.17.0 (2019-01-03)
|
||||
|
||||
### Added
|
||||
|
||||
- adds alder32sum function and test 6908fc2 (marshallford)
|
||||
- Added kebabcase function ca331a1 (Ilyes512)
|
||||
|
||||
### Changed
|
||||
|
||||
- Update goutils to 1.1.0 4e1125d (Matt Butcher)
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fix 'has' documentation e3f2a85 (dean-coakley)
|
||||
- docs(dict): fix typo in pick example dc424f9 (Dustin Specker)
|
||||
- fixes spelling errors... not sure how that happened 4cf188a (marshallford)
|
||||
|
||||
## Release 2.16.0 (2018-08-13)
|
||||
|
||||
### Added
|
||||
|
||||
- add splitn function fccb0b0 (Helgi Þorbjörnsson)
|
||||
- Add slice func df28ca7 (gongdo)
|
||||
- Generate serial number a3bdffd (Cody Coons)
|
||||
- Extract values of dict with values function df39312 (Lawrence Jones)
|
||||
|
||||
### Changed
|
||||
|
||||
- Modify panic message for list.slice ae38335 (gongdo)
|
||||
- Minor improvement in code quality - Removed an unreachable piece of code at defaults.go#L26:6 - Resolve formatting issues. 5834241 (Abhishek Kashyap)
|
||||
- Remove duplicated documentation 1d97af1 (Matthew Fisher)
|
||||
- Test on go 1.11 49df809 (Helgi Þormar Þorbjörnsson)
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fix file permissions c5f40b5 (gongdo)
|
||||
- Fix example for buildCustomCert 7779e0d (Tin Lam)
|
||||
|
||||
## Release 2.15.0 (2018-04-02)
|
||||
|
||||
### Added
|
||||
|
||||
- #68 and #69: Add json helpers to docs (thanks @arunvelsriram)
|
||||
- #66: Add ternary function (thanks @binoculars)
|
||||
- #67: Allow keys function to take multiple dicts (thanks @binoculars)
|
||||
- #89: Added sha1sum to crypto function (thanks @benkeil)
|
||||
- #81: Allow customizing Root CA that used by genSignedCert (thanks @chenzhiwei)
|
||||
- #92: Add travis testing for go 1.10
|
||||
- #93: Adding appveyor config for windows testing
|
||||
|
||||
### Changed
|
||||
|
||||
- #90: Updating to more recent dependencies
|
||||
- #73: replace satori/go.uuid with google/uuid (thanks @petterw)
|
||||
|
||||
### Fixed
|
||||
|
||||
- #76: Fixed documentation typos (thanks @Thiht)
|
||||
- Fixed rounding issue on the `ago` function. Note, the removes support for Go 1.8 and older
|
||||
|
||||
## Release 2.14.1 (2017-12-01)
|
||||
|
||||
### Fixed
|
||||
|
||||
- #60: Fix typo in function name documentation (thanks @neil-ca-moore)
|
||||
- #61: Removing line with {{ due to blocking github pages genertion
|
||||
- #64: Update the list functions to handle int, string, and other slices for compatibility
|
||||
|
||||
## Release 2.14.0 (2017-10-06)
|
||||
|
||||
This new version of Sprig adds a set of functions for generating and working with SSL certificates.
|
||||
|
||||
- `genCA` generates an SSL Certificate Authority
|
||||
- `genSelfSignedCert` generates an SSL self-signed certificate
|
||||
- `genSignedCert` generates an SSL certificate and key based on a given CA
|
||||
|
||||
## Release 2.13.0 (2017-09-18)
|
||||
|
||||
This release adds new functions, including:
|
||||
|
||||
- `regexMatch`, `regexFindAll`, `regexFind`, `regexReplaceAll`, `regexReplaceAllLiteral`, and `regexSplit` to work with regular expressions
|
||||
- `floor`, `ceil`, and `round` math functions
|
||||
- `toDate` converts a string to a date
|
||||
- `nindent` is just like `indent` but also prepends a new line
|
||||
- `ago` returns the time from `time.Now`
|
||||
|
||||
### Added
|
||||
|
||||
- #40: Added basic regex functionality (thanks @alanquillin)
|
||||
- #41: Added ceil floor and round functions (thanks @alanquillin)
|
||||
- #48: Added toDate function (thanks @andreynering)
|
||||
- #50: Added nindent function (thanks @binoculars)
|
||||
- #46: Added ago function (thanks @slayer)
|
||||
|
||||
### Changed
|
||||
|
||||
- #51: Updated godocs to include new string functions (thanks @curtisallen)
|
||||
- #49: Added ability to merge multiple dicts (thanks @binoculars)
|
||||
|
||||
## Release 2.12.0 (2017-05-17)
|
||||
|
||||
- `snakecase`, `camelcase`, and `shuffle` are three new string functions
|
||||
- `fail` allows you to bail out of a template render when conditions are not met
|
||||
|
||||
## Release 2.11.0 (2017-05-02)
|
||||
|
||||
- Added `toJson` and `toPrettyJson`
|
||||
- Added `merge`
|
||||
- Refactored documentation
|
||||
|
||||
## Release 2.10.0 (2017-03-15)
|
||||
|
||||
- Added `semver` and `semverCompare` for Semantic Versions
|
||||
- `list` replaces `tuple`
|
||||
- Fixed issue with `join`
|
||||
- Added `first`, `last`, `intial`, `rest`, `prepend`, `append`, `toString`, `toStrings`, `sortAlpha`, `reverse`, `coalesce`, `pluck`, `pick`, `compact`, `keys`, `omit`, `uniq`, `has`, `without`
|
||||
|
||||
## Release 2.9.0 (2017-02-23)
|
||||
|
||||
- Added `splitList` to split a list
|
||||
- Added crypto functions of `genPrivateKey` and `derivePassword`
|
||||
|
||||
## Release 2.8.0 (2016-12-21)
|
||||
|
||||
- Added access to several path functions (`base`, `dir`, `clean`, `ext`, and `abs`)
|
||||
- Added functions for _mutating_ dictionaries (`set`, `unset`, `hasKey`)
|
||||
|
||||
## Release 2.7.0 (2016-12-01)
|
||||
|
||||
- Added `sha256sum` to generate a hash of an input
|
||||
- Added functions to convert a numeric or string to `int`, `int64`, `float64`
|
||||
|
||||
## Release 2.6.0 (2016-10-03)
|
||||
|
||||
- Added a `uuidv4` template function for generating UUIDs inside of a template.
|
||||
|
||||
## Release 2.5.0 (2016-08-19)
|
||||
|
||||
- New `trimSuffix`, `trimPrefix`, `hasSuffix`, and `hasPrefix` functions
|
||||
- New aliases have been added for a few functions that didn't follow the naming conventions (`trimAll` and `abbrevBoth`)
|
||||
- `trimall` and `abbrevboth` (notice the case) are deprecated and will be removed in 3.0.0
|
||||
|
||||
## Release 2.4.0 (2016-08-16)
|
||||
|
||||
- Adds two functions: `until` and `untilStep`
|
||||
|
||||
## Release 2.3.0 (2016-06-21)
|
||||
|
||||
- cat: Concatenate strings with whitespace separators.
|
||||
- replace: Replace parts of a string: `replace " " "-" "Me First"` renders "Me-First"
|
||||
- plural: Format plurals: `len "foo" | plural "one foo" "many foos"` renders "many foos"
|
||||
- indent: Indent blocks of text in a way that is sensitive to "\n" characters.
|
||||
|
||||
## Release 2.2.0 (2016-04-21)
|
||||
|
||||
- Added a `genPrivateKey` function (Thanks @bacongobbler)
|
||||
|
||||
## Release 2.1.0 (2016-03-30)
|
||||
|
||||
- `default` now prints the default value when it does not receive a value down the pipeline. It is much safer now to do `{{.Foo | default "bar"}}`.
|
||||
- Added accessors for "hermetic" functions. These return only functions that, when given the same input, produce the same output.
|
||||
|
||||
## Release 2.0.0 (2016-03-29)
|
||||
|
||||
Because we switched from `int` to `int64` as the return value for all integer math functions, the library's major version number has been incremented.
|
||||
|
||||
- `min` complements `max` (formerly `biggest`)
|
||||
- `empty` indicates that a value is the empty value for its type
|
||||
- `tuple` creates a tuple inside of a template: `{{$t := tuple "a", "b" "c"}}`
|
||||
- `dict` creates a dictionary inside of a template `{{$d := dict "key1" "val1" "key2" "val2"}}`
|
||||
- Date formatters have been added for HTML dates (as used in `date` input fields)
|
||||
- Integer math functions can convert from a number of types, including `string` (via `strconv.ParseInt`).
|
||||
|
||||
## Release 1.2.0 (2016-02-01)
|
||||
|
||||
- Added quote and squote
|
||||
- Added b32enc and b32dec
|
||||
- add now takes varargs
|
||||
- biggest now takes varargs
|
||||
|
||||
## Release 1.1.0 (2015-12-29)
|
||||
|
||||
- Added #4: Added contains function. strings.Contains, but with the arguments
|
||||
switched to simplify common pipelines. (thanks krancour)
|
||||
- Added Travis-CI testing support
|
||||
|
||||
## Release 1.0.0 (2015-12-23)
|
||||
|
||||
- Initial release
|
||||
19
vendor/github.com/Masterminds/sprig/v3/LICENSE.txt
generated
vendored
Normal file
19
vendor/github.com/Masterminds/sprig/v3/LICENSE.txt
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
Copyright (C) 2013-2020 Masterminds
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
9
vendor/github.com/Masterminds/sprig/v3/Makefile
generated
vendored
Normal file
9
vendor/github.com/Masterminds/sprig/v3/Makefile
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
.PHONY: test
|
||||
test:
|
||||
@echo "==> Running tests"
|
||||
GO111MODULE=on go test -v
|
||||
|
||||
.PHONY: test-cover
|
||||
test-cover:
|
||||
@echo "==> Running Tests with coverage"
|
||||
GO111MODULE=on go test -cover .
|
||||
100
vendor/github.com/Masterminds/sprig/v3/README.md
generated
vendored
Normal file
100
vendor/github.com/Masterminds/sprig/v3/README.md
generated
vendored
Normal file
@@ -0,0 +1,100 @@
|
||||
# Sprig: Template functions for Go templates
|
||||
|
||||
[](https://pkg.go.dev/github.com/Masterminds/sprig/v3)
|
||||
[](https://goreportcard.com/report/github.com/Masterminds/sprig)
|
||||
[](https://masterminds.github.io/stability/sustained.html)
|
||||
[](https://github.com/Masterminds/sprig/actions)
|
||||
|
||||
The Go language comes with a [built-in template
|
||||
language](http://golang.org/pkg/text/template/), but not
|
||||
very many template functions. Sprig is a library that provides more than 100 commonly
|
||||
used template functions.
|
||||
|
||||
It is inspired by the template functions found in
|
||||
[Twig](http://twig.sensiolabs.org/documentation) and in various
|
||||
JavaScript libraries, such as [underscore.js](http://underscorejs.org/).
|
||||
|
||||
## IMPORTANT NOTES
|
||||
|
||||
Sprig leverages [mergo](https://github.com/imdario/mergo) to handle merges. In
|
||||
its v0.3.9 release, there was a behavior change that impacts merging template
|
||||
functions in sprig. It is currently recommended to use v0.3.10 or later of that package.
|
||||
Using v0.3.9 will cause sprig tests to fail.
|
||||
|
||||
## Package Versions
|
||||
|
||||
There are two active major versions of the `sprig` package.
|
||||
|
||||
* v3 is currently stable release series on the `master` branch. The Go API should
|
||||
remain compatible with v2, the current stable version. Behavior change behind
|
||||
some functions is the reason for the new major version.
|
||||
* v2 is the previous stable release series. It has been more than three years since
|
||||
the initial release of v2. You can read the documentation and see the code
|
||||
on the [release-2](https://github.com/Masterminds/sprig/tree/release-2) branch.
|
||||
Bug fixes to this major version will continue for some time.
|
||||
|
||||
## Usage
|
||||
|
||||
**Template developers**: Please use Sprig's [function documentation](http://masterminds.github.io/sprig/) for
|
||||
detailed instructions and code snippets for the >100 template functions available.
|
||||
|
||||
**Go developers**: If you'd like to include Sprig as a library in your program,
|
||||
our API documentation is available [at GoDoc.org](http://godoc.org/github.com/Masterminds/sprig).
|
||||
|
||||
For standard usage, read on.
|
||||
|
||||
### Load the Sprig library
|
||||
|
||||
To load the Sprig `FuncMap`:
|
||||
|
||||
```go
|
||||
|
||||
import (
|
||||
"github.com/Masterminds/sprig/v3"
|
||||
"html/template"
|
||||
)
|
||||
|
||||
// This example illustrates that the FuncMap *must* be set before the
|
||||
// templates themselves are loaded.
|
||||
tpl := template.Must(
|
||||
template.New("base").Funcs(sprig.FuncMap()).ParseGlob("*.html")
|
||||
)
|
||||
|
||||
|
||||
```
|
||||
|
||||
### Calling the functions inside of templates
|
||||
|
||||
By convention, all functions are lowercase. This seems to follow the Go
|
||||
idiom for template functions (as opposed to template methods, which are
|
||||
TitleCase). For example, this:
|
||||
|
||||
```
|
||||
{{ "hello!" | upper | repeat 5 }}
|
||||
```
|
||||
|
||||
produces this:
|
||||
|
||||
```
|
||||
HELLO!HELLO!HELLO!HELLO!HELLO!
|
||||
```
|
||||
|
||||
## Principles Driving Our Function Selection
|
||||
|
||||
We followed these principles to decide which functions to add and how to implement them:
|
||||
|
||||
- Use template functions to build layout. The following
|
||||
types of operations are within the domain of template functions:
|
||||
- Formatting
|
||||
- Layout
|
||||
- Simple type conversions
|
||||
- Utilities that assist in handling common formatting and layout needs (e.g. arithmetic)
|
||||
- Template functions should not return errors unless there is no way to print
|
||||
a sensible value. For example, converting a string to an integer should not
|
||||
produce an error if conversion fails. Instead, it should display a default
|
||||
value.
|
||||
- Simple math is necessary for grid layouts, pagers, and so on. Complex math
|
||||
(anything other than arithmetic) should be done outside of templates.
|
||||
- Template functions only deal with the data passed into them. They never retrieve
|
||||
data from a source.
|
||||
- Finally, do not override core Go template functions.
|
||||
653
vendor/github.com/Masterminds/sprig/v3/crypto.go
generated
vendored
Normal file
653
vendor/github.com/Masterminds/sprig/v3/crypto.go
generated
vendored
Normal file
@@ -0,0 +1,653 @@
|
||||
package sprig
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/dsa"
|
||||
"crypto/ecdsa"
|
||||
"crypto/ed25519"
|
||||
"crypto/elliptic"
|
||||
"crypto/hmac"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/sha1"
|
||||
"crypto/sha256"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/asn1"
|
||||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash/adler32"
|
||||
"io"
|
||||
"math/big"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
bcrypt_lib "golang.org/x/crypto/bcrypt"
|
||||
"golang.org/x/crypto/scrypt"
|
||||
)
|
||||
|
||||
func sha256sum(input string) string {
|
||||
hash := sha256.Sum256([]byte(input))
|
||||
return hex.EncodeToString(hash[:])
|
||||
}
|
||||
|
||||
func sha1sum(input string) string {
|
||||
hash := sha1.Sum([]byte(input))
|
||||
return hex.EncodeToString(hash[:])
|
||||
}
|
||||
|
||||
func adler32sum(input string) string {
|
||||
hash := adler32.Checksum([]byte(input))
|
||||
return fmt.Sprintf("%d", hash)
|
||||
}
|
||||
|
||||
func bcrypt(input string) string {
|
||||
hash, err := bcrypt_lib.GenerateFromPassword([]byte(input), bcrypt_lib.DefaultCost)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("failed to encrypt string with bcrypt: %s", err)
|
||||
}
|
||||
|
||||
return string(hash)
|
||||
}
|
||||
|
||||
func htpasswd(username string, password string) string {
|
||||
if strings.Contains(username, ":") {
|
||||
return fmt.Sprintf("invalid username: %s", username)
|
||||
}
|
||||
return fmt.Sprintf("%s:%s", username, bcrypt(password))
|
||||
}
|
||||
|
||||
func randBytes(count int) (string, error) {
|
||||
buf := make([]byte, count)
|
||||
if _, err := rand.Read(buf); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.StdEncoding.EncodeToString(buf), nil
|
||||
}
|
||||
|
||||
// uuidv4 provides a safe and secure UUID v4 implementation
|
||||
func uuidv4() string {
|
||||
return uuid.New().String()
|
||||
}
|
||||
|
||||
var masterPasswordSeed = "com.lyndir.masterpassword"
|
||||
|
||||
var passwordTypeTemplates = map[string][][]byte{
|
||||
"maximum": {[]byte("anoxxxxxxxxxxxxxxxxx"), []byte("axxxxxxxxxxxxxxxxxno")},
|
||||
"long": {[]byte("CvcvnoCvcvCvcv"), []byte("CvcvCvcvnoCvcv"), []byte("CvcvCvcvCvcvno"), []byte("CvccnoCvcvCvcv"), []byte("CvccCvcvnoCvcv"),
|
||||
[]byte("CvccCvcvCvcvno"), []byte("CvcvnoCvccCvcv"), []byte("CvcvCvccnoCvcv"), []byte("CvcvCvccCvcvno"), []byte("CvcvnoCvcvCvcc"),
|
||||
[]byte("CvcvCvcvnoCvcc"), []byte("CvcvCvcvCvccno"), []byte("CvccnoCvccCvcv"), []byte("CvccCvccnoCvcv"), []byte("CvccCvccCvcvno"),
|
||||
[]byte("CvcvnoCvccCvcc"), []byte("CvcvCvccnoCvcc"), []byte("CvcvCvccCvccno"), []byte("CvccnoCvcvCvcc"), []byte("CvccCvcvnoCvcc"),
|
||||
[]byte("CvccCvcvCvccno")},
|
||||
"medium": {[]byte("CvcnoCvc"), []byte("CvcCvcno")},
|
||||
"short": {[]byte("Cvcn")},
|
||||
"basic": {[]byte("aaanaaan"), []byte("aannaaan"), []byte("aaannaaa")},
|
||||
"pin": {[]byte("nnnn")},
|
||||
}
|
||||
|
||||
var templateCharacters = map[byte]string{
|
||||
'V': "AEIOU",
|
||||
'C': "BCDFGHJKLMNPQRSTVWXYZ",
|
||||
'v': "aeiou",
|
||||
'c': "bcdfghjklmnpqrstvwxyz",
|
||||
'A': "AEIOUBCDFGHJKLMNPQRSTVWXYZ",
|
||||
'a': "AEIOUaeiouBCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz",
|
||||
'n': "0123456789",
|
||||
'o': "@&%?,=[]_:-+*$#!'^~;()/.",
|
||||
'x': "AEIOUaeiouBCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz0123456789!@#$%^&*()",
|
||||
}
|
||||
|
||||
func derivePassword(counter uint32, passwordType, password, user, site string) string {
|
||||
var templates = passwordTypeTemplates[passwordType]
|
||||
if templates == nil {
|
||||
return fmt.Sprintf("cannot find password template %s", passwordType)
|
||||
}
|
||||
|
||||
var buffer bytes.Buffer
|
||||
buffer.WriteString(masterPasswordSeed)
|
||||
binary.Write(&buffer, binary.BigEndian, uint32(len(user)))
|
||||
buffer.WriteString(user)
|
||||
|
||||
salt := buffer.Bytes()
|
||||
key, err := scrypt.Key([]byte(password), salt, 32768, 8, 2, 64)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("failed to derive password: %s", err)
|
||||
}
|
||||
|
||||
buffer.Truncate(len(masterPasswordSeed))
|
||||
binary.Write(&buffer, binary.BigEndian, uint32(len(site)))
|
||||
buffer.WriteString(site)
|
||||
binary.Write(&buffer, binary.BigEndian, counter)
|
||||
|
||||
var hmacv = hmac.New(sha256.New, key)
|
||||
hmacv.Write(buffer.Bytes())
|
||||
var seed = hmacv.Sum(nil)
|
||||
var temp = templates[int(seed[0])%len(templates)]
|
||||
|
||||
buffer.Truncate(0)
|
||||
for i, element := range temp {
|
||||
passChars := templateCharacters[element]
|
||||
passChar := passChars[int(seed[i+1])%len(passChars)]
|
||||
buffer.WriteByte(passChar)
|
||||
}
|
||||
|
||||
return buffer.String()
|
||||
}
|
||||
|
||||
func generatePrivateKey(typ string) string {
|
||||
var priv interface{}
|
||||
var err error
|
||||
switch typ {
|
||||
case "", "rsa":
|
||||
// good enough for government work
|
||||
priv, err = rsa.GenerateKey(rand.Reader, 4096)
|
||||
case "dsa":
|
||||
key := new(dsa.PrivateKey)
|
||||
// again, good enough for government work
|
||||
if err = dsa.GenerateParameters(&key.Parameters, rand.Reader, dsa.L2048N256); err != nil {
|
||||
return fmt.Sprintf("failed to generate dsa params: %s", err)
|
||||
}
|
||||
err = dsa.GenerateKey(key, rand.Reader)
|
||||
priv = key
|
||||
case "ecdsa":
|
||||
// again, good enough for government work
|
||||
priv, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
case "ed25519":
|
||||
_, priv, err = ed25519.GenerateKey(rand.Reader)
|
||||
default:
|
||||
return "Unknown type " + typ
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Sprintf("failed to generate private key: %s", err)
|
||||
}
|
||||
|
||||
return string(pem.EncodeToMemory(pemBlockForKey(priv)))
|
||||
}
|
||||
|
||||
// DSAKeyFormat stores the format for DSA keys.
|
||||
// Used by pemBlockForKey
|
||||
type DSAKeyFormat struct {
|
||||
Version int
|
||||
P, Q, G, Y, X *big.Int
|
||||
}
|
||||
|
||||
func pemBlockForKey(priv interface{}) *pem.Block {
|
||||
switch k := priv.(type) {
|
||||
case *rsa.PrivateKey:
|
||||
return &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(k)}
|
||||
case *dsa.PrivateKey:
|
||||
val := DSAKeyFormat{
|
||||
P: k.P, Q: k.Q, G: k.G,
|
||||
Y: k.Y, X: k.X,
|
||||
}
|
||||
bytes, _ := asn1.Marshal(val)
|
||||
return &pem.Block{Type: "DSA PRIVATE KEY", Bytes: bytes}
|
||||
case *ecdsa.PrivateKey:
|
||||
b, _ := x509.MarshalECPrivateKey(k)
|
||||
return &pem.Block{Type: "EC PRIVATE KEY", Bytes: b}
|
||||
default:
|
||||
// attempt PKCS#8 format for all other keys
|
||||
b, err := x509.MarshalPKCS8PrivateKey(k)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return &pem.Block{Type: "PRIVATE KEY", Bytes: b}
|
||||
}
|
||||
}
|
||||
|
||||
func parsePrivateKeyPEM(pemBlock string) (crypto.PrivateKey, error) {
|
||||
block, _ := pem.Decode([]byte(pemBlock))
|
||||
if block == nil {
|
||||
return nil, errors.New("no PEM data in input")
|
||||
}
|
||||
|
||||
if block.Type == "PRIVATE KEY" {
|
||||
priv, err := x509.ParsePKCS8PrivateKey(block.Bytes)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decoding PEM as PKCS#8: %s", err)
|
||||
}
|
||||
return priv, nil
|
||||
} else if !strings.HasSuffix(block.Type, " PRIVATE KEY") {
|
||||
return nil, fmt.Errorf("no private key data in PEM block of type %s", block.Type)
|
||||
}
|
||||
|
||||
switch block.Type[:len(block.Type)-12] { // strip " PRIVATE KEY"
|
||||
case "RSA":
|
||||
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parsing RSA private key from PEM: %s", err)
|
||||
}
|
||||
return priv, nil
|
||||
case "EC":
|
||||
priv, err := x509.ParseECPrivateKey(block.Bytes)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parsing EC private key from PEM: %s", err)
|
||||
}
|
||||
return priv, nil
|
||||
case "DSA":
|
||||
var k DSAKeyFormat
|
||||
_, err := asn1.Unmarshal(block.Bytes, &k)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parsing DSA private key from PEM: %s", err)
|
||||
}
|
||||
priv := &dsa.PrivateKey{
|
||||
PublicKey: dsa.PublicKey{
|
||||
Parameters: dsa.Parameters{
|
||||
P: k.P, Q: k.Q, G: k.G,
|
||||
},
|
||||
Y: k.Y,
|
||||
},
|
||||
X: k.X,
|
||||
}
|
||||
return priv, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("invalid private key type %s", block.Type)
|
||||
}
|
||||
}
|
||||
|
||||
func getPublicKey(priv crypto.PrivateKey) (crypto.PublicKey, error) {
|
||||
switch k := priv.(type) {
|
||||
case interface{ Public() crypto.PublicKey }:
|
||||
return k.Public(), nil
|
||||
case *dsa.PrivateKey:
|
||||
return &k.PublicKey, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unable to get public key for type %T", priv)
|
||||
}
|
||||
}
|
||||
|
||||
type certificate struct {
|
||||
Cert string
|
||||
Key string
|
||||
}
|
||||
|
||||
func buildCustomCertificate(b64cert string, b64key string) (certificate, error) {
|
||||
crt := certificate{}
|
||||
|
||||
cert, err := base64.StdEncoding.DecodeString(b64cert)
|
||||
if err != nil {
|
||||
return crt, errors.New("unable to decode base64 certificate")
|
||||
}
|
||||
|
||||
key, err := base64.StdEncoding.DecodeString(b64key)
|
||||
if err != nil {
|
||||
return crt, errors.New("unable to decode base64 private key")
|
||||
}
|
||||
|
||||
decodedCert, _ := pem.Decode(cert)
|
||||
if decodedCert == nil {
|
||||
return crt, errors.New("unable to decode certificate")
|
||||
}
|
||||
_, err = x509.ParseCertificate(decodedCert.Bytes)
|
||||
if err != nil {
|
||||
return crt, fmt.Errorf(
|
||||
"error parsing certificate: decodedCert.Bytes: %s",
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
_, err = parsePrivateKeyPEM(string(key))
|
||||
if err != nil {
|
||||
return crt, fmt.Errorf(
|
||||
"error parsing private key: %s",
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
crt.Cert = string(cert)
|
||||
crt.Key = string(key)
|
||||
|
||||
return crt, nil
|
||||
}
|
||||
|
||||
func generateCertificateAuthority(
|
||||
cn string,
|
||||
daysValid int,
|
||||
) (certificate, error) {
|
||||
priv, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
if err != nil {
|
||||
return certificate{}, fmt.Errorf("error generating rsa key: %s", err)
|
||||
}
|
||||
|
||||
return generateCertificateAuthorityWithKeyInternal(cn, daysValid, priv)
|
||||
}
|
||||
|
||||
func generateCertificateAuthorityWithPEMKey(
|
||||
cn string,
|
||||
daysValid int,
|
||||
privPEM string,
|
||||
) (certificate, error) {
|
||||
priv, err := parsePrivateKeyPEM(privPEM)
|
||||
if err != nil {
|
||||
return certificate{}, fmt.Errorf("parsing private key: %s", err)
|
||||
}
|
||||
return generateCertificateAuthorityWithKeyInternal(cn, daysValid, priv)
|
||||
}
|
||||
|
||||
func generateCertificateAuthorityWithKeyInternal(
|
||||
cn string,
|
||||
daysValid int,
|
||||
priv crypto.PrivateKey,
|
||||
) (certificate, error) {
|
||||
ca := certificate{}
|
||||
|
||||
template, err := getBaseCertTemplate(cn, nil, nil, daysValid)
|
||||
if err != nil {
|
||||
return ca, err
|
||||
}
|
||||
// Override KeyUsage and IsCA
|
||||
template.KeyUsage = x509.KeyUsageKeyEncipherment |
|
||||
x509.KeyUsageDigitalSignature |
|
||||
x509.KeyUsageCertSign
|
||||
template.IsCA = true
|
||||
|
||||
ca.Cert, ca.Key, err = getCertAndKey(template, priv, template, priv)
|
||||
|
||||
return ca, err
|
||||
}
|
||||
|
||||
func generateSelfSignedCertificate(
|
||||
cn string,
|
||||
ips []interface{},
|
||||
alternateDNS []interface{},
|
||||
daysValid int,
|
||||
) (certificate, error) {
|
||||
priv, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
if err != nil {
|
||||
return certificate{}, fmt.Errorf("error generating rsa key: %s", err)
|
||||
}
|
||||
return generateSelfSignedCertificateWithKeyInternal(cn, ips, alternateDNS, daysValid, priv)
|
||||
}
|
||||
|
||||
func generateSelfSignedCertificateWithPEMKey(
|
||||
cn string,
|
||||
ips []interface{},
|
||||
alternateDNS []interface{},
|
||||
daysValid int,
|
||||
privPEM string,
|
||||
) (certificate, error) {
|
||||
priv, err := parsePrivateKeyPEM(privPEM)
|
||||
if err != nil {
|
||||
return certificate{}, fmt.Errorf("parsing private key: %s", err)
|
||||
}
|
||||
return generateSelfSignedCertificateWithKeyInternal(cn, ips, alternateDNS, daysValid, priv)
|
||||
}
|
||||
|
||||
func generateSelfSignedCertificateWithKeyInternal(
|
||||
cn string,
|
||||
ips []interface{},
|
||||
alternateDNS []interface{},
|
||||
daysValid int,
|
||||
priv crypto.PrivateKey,
|
||||
) (certificate, error) {
|
||||
cert := certificate{}
|
||||
|
||||
template, err := getBaseCertTemplate(cn, ips, alternateDNS, daysValid)
|
||||
if err != nil {
|
||||
return cert, err
|
||||
}
|
||||
|
||||
cert.Cert, cert.Key, err = getCertAndKey(template, priv, template, priv)
|
||||
|
||||
return cert, err
|
||||
}
|
||||
|
||||
func generateSignedCertificate(
|
||||
cn string,
|
||||
ips []interface{},
|
||||
alternateDNS []interface{},
|
||||
daysValid int,
|
||||
ca certificate,
|
||||
) (certificate, error) {
|
||||
priv, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
if err != nil {
|
||||
return certificate{}, fmt.Errorf("error generating rsa key: %s", err)
|
||||
}
|
||||
return generateSignedCertificateWithKeyInternal(cn, ips, alternateDNS, daysValid, ca, priv)
|
||||
}
|
||||
|
||||
func generateSignedCertificateWithPEMKey(
|
||||
cn string,
|
||||
ips []interface{},
|
||||
alternateDNS []interface{},
|
||||
daysValid int,
|
||||
ca certificate,
|
||||
privPEM string,
|
||||
) (certificate, error) {
|
||||
priv, err := parsePrivateKeyPEM(privPEM)
|
||||
if err != nil {
|
||||
return certificate{}, fmt.Errorf("parsing private key: %s", err)
|
||||
}
|
||||
return generateSignedCertificateWithKeyInternal(cn, ips, alternateDNS, daysValid, ca, priv)
|
||||
}
|
||||
|
||||
func generateSignedCertificateWithKeyInternal(
|
||||
cn string,
|
||||
ips []interface{},
|
||||
alternateDNS []interface{},
|
||||
daysValid int,
|
||||
ca certificate,
|
||||
priv crypto.PrivateKey,
|
||||
) (certificate, error) {
|
||||
cert := certificate{}
|
||||
|
||||
decodedSignerCert, _ := pem.Decode([]byte(ca.Cert))
|
||||
if decodedSignerCert == nil {
|
||||
return cert, errors.New("unable to decode certificate")
|
||||
}
|
||||
signerCert, err := x509.ParseCertificate(decodedSignerCert.Bytes)
|
||||
if err != nil {
|
||||
return cert, fmt.Errorf(
|
||||
"error parsing certificate: decodedSignerCert.Bytes: %s",
|
||||
err,
|
||||
)
|
||||
}
|
||||
signerKey, err := parsePrivateKeyPEM(ca.Key)
|
||||
if err != nil {
|
||||
return cert, fmt.Errorf(
|
||||
"error parsing private key: %s",
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
template, err := getBaseCertTemplate(cn, ips, alternateDNS, daysValid)
|
||||
if err != nil {
|
||||
return cert, err
|
||||
}
|
||||
|
||||
cert.Cert, cert.Key, err = getCertAndKey(
|
||||
template,
|
||||
priv,
|
||||
signerCert,
|
||||
signerKey,
|
||||
)
|
||||
|
||||
return cert, err
|
||||
}
|
||||
|
||||
func getCertAndKey(
|
||||
template *x509.Certificate,
|
||||
signeeKey crypto.PrivateKey,
|
||||
parent *x509.Certificate,
|
||||
signingKey crypto.PrivateKey,
|
||||
) (string, string, error) {
|
||||
signeePubKey, err := getPublicKey(signeeKey)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("error retrieving public key from signee key: %s", err)
|
||||
}
|
||||
derBytes, err := x509.CreateCertificate(
|
||||
rand.Reader,
|
||||
template,
|
||||
parent,
|
||||
signeePubKey,
|
||||
signingKey,
|
||||
)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("error creating certificate: %s", err)
|
||||
}
|
||||
|
||||
certBuffer := bytes.Buffer{}
|
||||
if err := pem.Encode(
|
||||
&certBuffer,
|
||||
&pem.Block{Type: "CERTIFICATE", Bytes: derBytes},
|
||||
); err != nil {
|
||||
return "", "", fmt.Errorf("error pem-encoding certificate: %s", err)
|
||||
}
|
||||
|
||||
keyBuffer := bytes.Buffer{}
|
||||
if err := pem.Encode(
|
||||
&keyBuffer,
|
||||
pemBlockForKey(signeeKey),
|
||||
); err != nil {
|
||||
return "", "", fmt.Errorf("error pem-encoding key: %s", err)
|
||||
}
|
||||
|
||||
return certBuffer.String(), keyBuffer.String(), nil
|
||||
}
|
||||
|
||||
func getBaseCertTemplate(
|
||||
cn string,
|
||||
ips []interface{},
|
||||
alternateDNS []interface{},
|
||||
daysValid int,
|
||||
) (*x509.Certificate, error) {
|
||||
ipAddresses, err := getNetIPs(ips)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dnsNames, err := getAlternateDNSStrs(alternateDNS)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
serialNumberUpperBound := new(big.Int).Lsh(big.NewInt(1), 128)
|
||||
serialNumber, err := rand.Int(rand.Reader, serialNumberUpperBound)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &x509.Certificate{
|
||||
SerialNumber: serialNumber,
|
||||
Subject: pkix.Name{
|
||||
CommonName: cn,
|
||||
},
|
||||
IPAddresses: ipAddresses,
|
||||
DNSNames: dnsNames,
|
||||
NotBefore: time.Now(),
|
||||
NotAfter: time.Now().Add(time.Hour * 24 * time.Duration(daysValid)),
|
||||
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
||||
ExtKeyUsage: []x509.ExtKeyUsage{
|
||||
x509.ExtKeyUsageServerAuth,
|
||||
x509.ExtKeyUsageClientAuth,
|
||||
},
|
||||
BasicConstraintsValid: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func getNetIPs(ips []interface{}) ([]net.IP, error) {
|
||||
if ips == nil {
|
||||
return []net.IP{}, nil
|
||||
}
|
||||
var ipStr string
|
||||
var ok bool
|
||||
var netIP net.IP
|
||||
netIPs := make([]net.IP, len(ips))
|
||||
for i, ip := range ips {
|
||||
ipStr, ok = ip.(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("error parsing ip: %v is not a string", ip)
|
||||
}
|
||||
netIP = net.ParseIP(ipStr)
|
||||
if netIP == nil {
|
||||
return nil, fmt.Errorf("error parsing ip: %s", ipStr)
|
||||
}
|
||||
netIPs[i] = netIP
|
||||
}
|
||||
return netIPs, nil
|
||||
}
|
||||
|
||||
func getAlternateDNSStrs(alternateDNS []interface{}) ([]string, error) {
|
||||
if alternateDNS == nil {
|
||||
return []string{}, nil
|
||||
}
|
||||
var dnsStr string
|
||||
var ok bool
|
||||
alternateDNSStrs := make([]string, len(alternateDNS))
|
||||
for i, dns := range alternateDNS {
|
||||
dnsStr, ok = dns.(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf(
|
||||
"error processing alternate dns name: %v is not a string",
|
||||
dns,
|
||||
)
|
||||
}
|
||||
alternateDNSStrs[i] = dnsStr
|
||||
}
|
||||
return alternateDNSStrs, nil
|
||||
}
|
||||
|
||||
func encryptAES(password string, plaintext string) (string, error) {
|
||||
if plaintext == "" {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
key := make([]byte, 32)
|
||||
copy(key, []byte(password))
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
content := []byte(plaintext)
|
||||
blockSize := block.BlockSize()
|
||||
padding := blockSize - len(content)%blockSize
|
||||
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
||||
content = append(content, padtext...)
|
||||
|
||||
ciphertext := make([]byte, aes.BlockSize+len(content))
|
||||
|
||||
iv := ciphertext[:aes.BlockSize]
|
||||
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
mode := cipher.NewCBCEncrypter(block, iv)
|
||||
mode.CryptBlocks(ciphertext[aes.BlockSize:], content)
|
||||
|
||||
return base64.StdEncoding.EncodeToString(ciphertext), nil
|
||||
}
|
||||
|
||||
func decryptAES(password string, crypt64 string) (string, error) {
|
||||
if crypt64 == "" {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
key := make([]byte, 32)
|
||||
copy(key, []byte(password))
|
||||
|
||||
crypt, err := base64.StdEncoding.DecodeString(crypt64)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
iv := crypt[:aes.BlockSize]
|
||||
crypt = crypt[aes.BlockSize:]
|
||||
decrypted := make([]byte, len(crypt))
|
||||
mode := cipher.NewCBCDecrypter(block, iv)
|
||||
mode.CryptBlocks(decrypted, crypt)
|
||||
|
||||
return string(decrypted[:len(decrypted)-int(decrypted[len(decrypted)-1])]), nil
|
||||
}
|
||||
152
vendor/github.com/Masterminds/sprig/v3/date.go
generated
vendored
Normal file
152
vendor/github.com/Masterminds/sprig/v3/date.go
generated
vendored
Normal file
@@ -0,0 +1,152 @@
|
||||
package sprig
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Given a format and a date, format the date string.
|
||||
//
|
||||
// Date can be a `time.Time` or an `int, int32, int64`.
|
||||
// In the later case, it is treated as seconds since UNIX
|
||||
// epoch.
|
||||
func date(fmt string, date interface{}) string {
|
||||
return dateInZone(fmt, date, "Local")
|
||||
}
|
||||
|
||||
func htmlDate(date interface{}) string {
|
||||
return dateInZone("2006-01-02", date, "Local")
|
||||
}
|
||||
|
||||
func htmlDateInZone(date interface{}, zone string) string {
|
||||
return dateInZone("2006-01-02", date, zone)
|
||||
}
|
||||
|
||||
func dateInZone(fmt string, date interface{}, zone string) string {
|
||||
var t time.Time
|
||||
switch date := date.(type) {
|
||||
default:
|
||||
t = time.Now()
|
||||
case time.Time:
|
||||
t = date
|
||||
case *time.Time:
|
||||
t = *date
|
||||
case int64:
|
||||
t = time.Unix(date, 0)
|
||||
case int:
|
||||
t = time.Unix(int64(date), 0)
|
||||
case int32:
|
||||
t = time.Unix(int64(date), 0)
|
||||
}
|
||||
|
||||
loc, err := time.LoadLocation(zone)
|
||||
if err != nil {
|
||||
loc, _ = time.LoadLocation("UTC")
|
||||
}
|
||||
|
||||
return t.In(loc).Format(fmt)
|
||||
}
|
||||
|
||||
func dateModify(fmt string, date time.Time) time.Time {
|
||||
d, err := time.ParseDuration(fmt)
|
||||
if err != nil {
|
||||
return date
|
||||
}
|
||||
return date.Add(d)
|
||||
}
|
||||
|
||||
func mustDateModify(fmt string, date time.Time) (time.Time, error) {
|
||||
d, err := time.ParseDuration(fmt)
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
return date.Add(d), nil
|
||||
}
|
||||
|
||||
func dateAgo(date interface{}) string {
|
||||
var t time.Time
|
||||
|
||||
switch date := date.(type) {
|
||||
default:
|
||||
t = time.Now()
|
||||
case time.Time:
|
||||
t = date
|
||||
case int64:
|
||||
t = time.Unix(date, 0)
|
||||
case int:
|
||||
t = time.Unix(int64(date), 0)
|
||||
}
|
||||
// Drop resolution to seconds
|
||||
duration := time.Since(t).Round(time.Second)
|
||||
return duration.String()
|
||||
}
|
||||
|
||||
func duration(sec interface{}) string {
|
||||
var n int64
|
||||
switch value := sec.(type) {
|
||||
default:
|
||||
n = 0
|
||||
case string:
|
||||
n, _ = strconv.ParseInt(value, 10, 64)
|
||||
case int64:
|
||||
n = value
|
||||
}
|
||||
return (time.Duration(n) * time.Second).String()
|
||||
}
|
||||
|
||||
func durationRound(duration interface{}) string {
|
||||
var d time.Duration
|
||||
switch duration := duration.(type) {
|
||||
default:
|
||||
d = 0
|
||||
case string:
|
||||
d, _ = time.ParseDuration(duration)
|
||||
case int64:
|
||||
d = time.Duration(duration)
|
||||
case time.Time:
|
||||
d = time.Since(duration)
|
||||
}
|
||||
|
||||
u := uint64(d)
|
||||
neg := d < 0
|
||||
if neg {
|
||||
u = -u
|
||||
}
|
||||
|
||||
var (
|
||||
year = uint64(time.Hour) * 24 * 365
|
||||
month = uint64(time.Hour) * 24 * 30
|
||||
day = uint64(time.Hour) * 24
|
||||
hour = uint64(time.Hour)
|
||||
minute = uint64(time.Minute)
|
||||
second = uint64(time.Second)
|
||||
)
|
||||
switch {
|
||||
case u > year:
|
||||
return strconv.FormatUint(u/year, 10) + "y"
|
||||
case u > month:
|
||||
return strconv.FormatUint(u/month, 10) + "mo"
|
||||
case u > day:
|
||||
return strconv.FormatUint(u/day, 10) + "d"
|
||||
case u > hour:
|
||||
return strconv.FormatUint(u/hour, 10) + "h"
|
||||
case u > minute:
|
||||
return strconv.FormatUint(u/minute, 10) + "m"
|
||||
case u > second:
|
||||
return strconv.FormatUint(u/second, 10) + "s"
|
||||
}
|
||||
return "0s"
|
||||
}
|
||||
|
||||
func toDate(fmt, str string) time.Time {
|
||||
t, _ := time.ParseInLocation(fmt, str, time.Local)
|
||||
return t
|
||||
}
|
||||
|
||||
func mustToDate(fmt, str string) (time.Time, error) {
|
||||
return time.ParseInLocation(fmt, str, time.Local)
|
||||
}
|
||||
|
||||
func unixEpoch(date time.Time) string {
|
||||
return strconv.FormatInt(date.Unix(), 10)
|
||||
}
|
||||
163
vendor/github.com/Masterminds/sprig/v3/defaults.go
generated
vendored
Normal file
163
vendor/github.com/Masterminds/sprig/v3/defaults.go
generated
vendored
Normal file
@@ -0,0 +1,163 @@
|
||||
package sprig
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
}
|
||||
|
||||
// dfault checks whether `given` is set, and returns default if not set.
|
||||
//
|
||||
// This returns `d` if `given` appears not to be set, and `given` otherwise.
|
||||
//
|
||||
// For numeric types 0 is unset.
|
||||
// For strings, maps, arrays, and slices, len() = 0 is considered unset.
|
||||
// For bool, false is unset.
|
||||
// Structs are never considered unset.
|
||||
//
|
||||
// For everything else, including pointers, a nil value is unset.
|
||||
func dfault(d interface{}, given ...interface{}) interface{} {
|
||||
|
||||
if empty(given) || empty(given[0]) {
|
||||
return d
|
||||
}
|
||||
return given[0]
|
||||
}
|
||||
|
||||
// empty returns true if the given value has the zero value for its type.
|
||||
func empty(given interface{}) bool {
|
||||
g := reflect.ValueOf(given)
|
||||
if !g.IsValid() {
|
||||
return true
|
||||
}
|
||||
|
||||
// Basically adapted from text/template.isTrue
|
||||
switch g.Kind() {
|
||||
default:
|
||||
return g.IsNil()
|
||||
case reflect.Array, reflect.Slice, reflect.Map, reflect.String:
|
||||
return g.Len() == 0
|
||||
case reflect.Bool:
|
||||
return !g.Bool()
|
||||
case reflect.Complex64, reflect.Complex128:
|
||||
return g.Complex() == 0
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return g.Int() == 0
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
return g.Uint() == 0
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return g.Float() == 0
|
||||
case reflect.Struct:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// coalesce returns the first non-empty value.
|
||||
func coalesce(v ...interface{}) interface{} {
|
||||
for _, val := range v {
|
||||
if !empty(val) {
|
||||
return val
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// all returns true if empty(x) is false for all values x in the list.
|
||||
// If the list is empty, return true.
|
||||
func all(v ...interface{}) bool {
|
||||
for _, val := range v {
|
||||
if empty(val) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// any returns true if empty(x) is false for any x in the list.
|
||||
// If the list is empty, return false.
|
||||
func any(v ...interface{}) bool {
|
||||
for _, val := range v {
|
||||
if !empty(val) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// fromJson decodes JSON into a structured value, ignoring errors.
|
||||
func fromJson(v string) interface{} {
|
||||
output, _ := mustFromJson(v)
|
||||
return output
|
||||
}
|
||||
|
||||
// mustFromJson decodes JSON into a structured value, returning errors.
|
||||
func mustFromJson(v string) (interface{}, error) {
|
||||
var output interface{}
|
||||
err := json.Unmarshal([]byte(v), &output)
|
||||
return output, err
|
||||
}
|
||||
|
||||
// toJson encodes an item into a JSON string
|
||||
func toJson(v interface{}) string {
|
||||
output, _ := json.Marshal(v)
|
||||
return string(output)
|
||||
}
|
||||
|
||||
func mustToJson(v interface{}) (string, error) {
|
||||
output, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(output), nil
|
||||
}
|
||||
|
||||
// toPrettyJson encodes an item into a pretty (indented) JSON string
|
||||
func toPrettyJson(v interface{}) string {
|
||||
output, _ := json.MarshalIndent(v, "", " ")
|
||||
return string(output)
|
||||
}
|
||||
|
||||
func mustToPrettyJson(v interface{}) (string, error) {
|
||||
output, err := json.MarshalIndent(v, "", " ")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(output), nil
|
||||
}
|
||||
|
||||
// toRawJson encodes an item into a JSON string with no escaping of HTML characters.
|
||||
func toRawJson(v interface{}) string {
|
||||
output, err := mustToRawJson(v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return string(output)
|
||||
}
|
||||
|
||||
// mustToRawJson encodes an item into a JSON string with no escaping of HTML characters.
|
||||
func mustToRawJson(v interface{}) (string, error) {
|
||||
buf := new(bytes.Buffer)
|
||||
enc := json.NewEncoder(buf)
|
||||
enc.SetEscapeHTML(false)
|
||||
err := enc.Encode(&v)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return strings.TrimSuffix(buf.String(), "\n"), nil
|
||||
}
|
||||
|
||||
// ternary returns the first value if the last value is true, otherwise returns the second value.
|
||||
func ternary(vt interface{}, vf interface{}, v bool) interface{} {
|
||||
if v {
|
||||
return vt
|
||||
}
|
||||
|
||||
return vf
|
||||
}
|
||||
174
vendor/github.com/Masterminds/sprig/v3/dict.go
generated
vendored
Normal file
174
vendor/github.com/Masterminds/sprig/v3/dict.go
generated
vendored
Normal file
@@ -0,0 +1,174 @@
|
||||
package sprig
|
||||
|
||||
import (
|
||||
"github.com/imdario/mergo"
|
||||
"github.com/mitchellh/copystructure"
|
||||
)
|
||||
|
||||
func get(d map[string]interface{}, key string) interface{} {
|
||||
if val, ok := d[key]; ok {
|
||||
return val
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func set(d map[string]interface{}, key string, value interface{}) map[string]interface{} {
|
||||
d[key] = value
|
||||
return d
|
||||
}
|
||||
|
||||
func unset(d map[string]interface{}, key string) map[string]interface{} {
|
||||
delete(d, key)
|
||||
return d
|
||||
}
|
||||
|
||||
func hasKey(d map[string]interface{}, key string) bool {
|
||||
_, ok := d[key]
|
||||
return ok
|
||||
}
|
||||
|
||||
func pluck(key string, d ...map[string]interface{}) []interface{} {
|
||||
res := []interface{}{}
|
||||
for _, dict := range d {
|
||||
if val, ok := dict[key]; ok {
|
||||
res = append(res, val)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func keys(dicts ...map[string]interface{}) []string {
|
||||
k := []string{}
|
||||
for _, dict := range dicts {
|
||||
for key := range dict {
|
||||
k = append(k, key)
|
||||
}
|
||||
}
|
||||
return k
|
||||
}
|
||||
|
||||
func pick(dict map[string]interface{}, keys ...string) map[string]interface{} {
|
||||
res := map[string]interface{}{}
|
||||
for _, k := range keys {
|
||||
if v, ok := dict[k]; ok {
|
||||
res[k] = v
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func omit(dict map[string]interface{}, keys ...string) map[string]interface{} {
|
||||
res := map[string]interface{}{}
|
||||
|
||||
omit := make(map[string]bool, len(keys))
|
||||
for _, k := range keys {
|
||||
omit[k] = true
|
||||
}
|
||||
|
||||
for k, v := range dict {
|
||||
if _, ok := omit[k]; !ok {
|
||||
res[k] = v
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func dict(v ...interface{}) map[string]interface{} {
|
||||
dict := map[string]interface{}{}
|
||||
lenv := len(v)
|
||||
for i := 0; i < lenv; i += 2 {
|
||||
key := strval(v[i])
|
||||
if i+1 >= lenv {
|
||||
dict[key] = ""
|
||||
continue
|
||||
}
|
||||
dict[key] = v[i+1]
|
||||
}
|
||||
return dict
|
||||
}
|
||||
|
||||
func merge(dst map[string]interface{}, srcs ...map[string]interface{}) interface{} {
|
||||
for _, src := range srcs {
|
||||
if err := mergo.Merge(&dst, src); err != nil {
|
||||
// Swallow errors inside of a template.
|
||||
return ""
|
||||
}
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
func mustMerge(dst map[string]interface{}, srcs ...map[string]interface{}) (interface{}, error) {
|
||||
for _, src := range srcs {
|
||||
if err := mergo.Merge(&dst, src); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return dst, nil
|
||||
}
|
||||
|
||||
func mergeOverwrite(dst map[string]interface{}, srcs ...map[string]interface{}) interface{} {
|
||||
for _, src := range srcs {
|
||||
if err := mergo.MergeWithOverwrite(&dst, src); err != nil {
|
||||
// Swallow errors inside of a template.
|
||||
return ""
|
||||
}
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
func mustMergeOverwrite(dst map[string]interface{}, srcs ...map[string]interface{}) (interface{}, error) {
|
||||
for _, src := range srcs {
|
||||
if err := mergo.MergeWithOverwrite(&dst, src); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return dst, nil
|
||||
}
|
||||
|
||||
func values(dict map[string]interface{}) []interface{} {
|
||||
values := []interface{}{}
|
||||
for _, value := range dict {
|
||||
values = append(values, value)
|
||||
}
|
||||
|
||||
return values
|
||||
}
|
||||
|
||||
func deepCopy(i interface{}) interface{} {
|
||||
c, err := mustDeepCopy(i)
|
||||
if err != nil {
|
||||
panic("deepCopy error: " + err.Error())
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func mustDeepCopy(i interface{}) (interface{}, error) {
|
||||
return copystructure.Copy(i)
|
||||
}
|
||||
|
||||
func dig(ps ...interface{}) (interface{}, error) {
|
||||
if len(ps) < 3 {
|
||||
panic("dig needs at least three arguments")
|
||||
}
|
||||
dict := ps[len(ps)-1].(map[string]interface{})
|
||||
def := ps[len(ps)-2]
|
||||
ks := make([]string, len(ps)-2)
|
||||
for i := 0; i < len(ks); i++ {
|
||||
ks[i] = ps[i].(string)
|
||||
}
|
||||
|
||||
return digFromDict(dict, def, ks)
|
||||
}
|
||||
|
||||
func digFromDict(dict map[string]interface{}, d interface{}, ks []string) (interface{}, error) {
|
||||
k, ns := ks[0], ks[1:len(ks)]
|
||||
step, has := dict[k]
|
||||
if !has {
|
||||
return d, nil
|
||||
}
|
||||
if len(ns) == 0 {
|
||||
return step, nil
|
||||
}
|
||||
return digFromDict(step.(map[string]interface{}), d, ns)
|
||||
}
|
||||
19
vendor/github.com/Masterminds/sprig/v3/doc.go
generated
vendored
Normal file
19
vendor/github.com/Masterminds/sprig/v3/doc.go
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
Package sprig provides template functions for Go.
|
||||
|
||||
This package contains a number of utility functions for working with data
|
||||
inside of Go `html/template` and `text/template` files.
|
||||
|
||||
To add these functions, use the `template.Funcs()` method:
|
||||
|
||||
t := templates.New("foo").Funcs(sprig.FuncMap())
|
||||
|
||||
Note that you should add the function map before you parse any template files.
|
||||
|
||||
In several cases, Sprig reverses the order of arguments from the way they
|
||||
appear in the standard library. This is to make it easier to pipe
|
||||
arguments into functions.
|
||||
|
||||
See http://masterminds.github.io/sprig/ for more detailed documentation on each of the available functions.
|
||||
*/
|
||||
package sprig
|
||||
382
vendor/github.com/Masterminds/sprig/v3/functions.go
generated
vendored
Normal file
382
vendor/github.com/Masterminds/sprig/v3/functions.go
generated
vendored
Normal file
@@ -0,0 +1,382 @@
|
||||
package sprig
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"html/template"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
ttemplate "text/template"
|
||||
"time"
|
||||
|
||||
util "github.com/Masterminds/goutils"
|
||||
"github.com/huandu/xstrings"
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
// FuncMap produces the function map.
|
||||
//
|
||||
// Use this to pass the functions into the template engine:
|
||||
//
|
||||
// tpl := template.New("foo").Funcs(sprig.FuncMap()))
|
||||
//
|
||||
func FuncMap() template.FuncMap {
|
||||
return HtmlFuncMap()
|
||||
}
|
||||
|
||||
// HermeticTxtFuncMap returns a 'text/template'.FuncMap with only repeatable functions.
|
||||
func HermeticTxtFuncMap() ttemplate.FuncMap {
|
||||
r := TxtFuncMap()
|
||||
for _, name := range nonhermeticFunctions {
|
||||
delete(r, name)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// HermeticHtmlFuncMap returns an 'html/template'.Funcmap with only repeatable functions.
|
||||
func HermeticHtmlFuncMap() template.FuncMap {
|
||||
r := HtmlFuncMap()
|
||||
for _, name := range nonhermeticFunctions {
|
||||
delete(r, name)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// TxtFuncMap returns a 'text/template'.FuncMap
|
||||
func TxtFuncMap() ttemplate.FuncMap {
|
||||
return ttemplate.FuncMap(GenericFuncMap())
|
||||
}
|
||||
|
||||
// HtmlFuncMap returns an 'html/template'.Funcmap
|
||||
func HtmlFuncMap() template.FuncMap {
|
||||
return template.FuncMap(GenericFuncMap())
|
||||
}
|
||||
|
||||
// GenericFuncMap returns a copy of the basic function map as a map[string]interface{}.
|
||||
func GenericFuncMap() map[string]interface{} {
|
||||
gfm := make(map[string]interface{}, len(genericMap))
|
||||
for k, v := range genericMap {
|
||||
gfm[k] = v
|
||||
}
|
||||
return gfm
|
||||
}
|
||||
|
||||
// These functions are not guaranteed to evaluate to the same result for given input, because they
|
||||
// refer to the environment or global state.
|
||||
var nonhermeticFunctions = []string{
|
||||
// Date functions
|
||||
"date",
|
||||
"date_in_zone",
|
||||
"date_modify",
|
||||
"now",
|
||||
"htmlDate",
|
||||
"htmlDateInZone",
|
||||
"dateInZone",
|
||||
"dateModify",
|
||||
|
||||
// Strings
|
||||
"randAlphaNum",
|
||||
"randAlpha",
|
||||
"randAscii",
|
||||
"randNumeric",
|
||||
"randBytes",
|
||||
"uuidv4",
|
||||
|
||||
// OS
|
||||
"env",
|
||||
"expandenv",
|
||||
|
||||
// Network
|
||||
"getHostByName",
|
||||
}
|
||||
|
||||
var genericMap = map[string]interface{}{
|
||||
"hello": func() string { return "Hello!" },
|
||||
|
||||
// Date functions
|
||||
"ago": dateAgo,
|
||||
"date": date,
|
||||
"date_in_zone": dateInZone,
|
||||
"date_modify": dateModify,
|
||||
"dateInZone": dateInZone,
|
||||
"dateModify": dateModify,
|
||||
"duration": duration,
|
||||
"durationRound": durationRound,
|
||||
"htmlDate": htmlDate,
|
||||
"htmlDateInZone": htmlDateInZone,
|
||||
"must_date_modify": mustDateModify,
|
||||
"mustDateModify": mustDateModify,
|
||||
"mustToDate": mustToDate,
|
||||
"now": time.Now,
|
||||
"toDate": toDate,
|
||||
"unixEpoch": unixEpoch,
|
||||
|
||||
// Strings
|
||||
"abbrev": abbrev,
|
||||
"abbrevboth": abbrevboth,
|
||||
"trunc": trunc,
|
||||
"trim": strings.TrimSpace,
|
||||
"upper": strings.ToUpper,
|
||||
"lower": strings.ToLower,
|
||||
"title": strings.Title,
|
||||
"untitle": untitle,
|
||||
"substr": substring,
|
||||
// Switch order so that "foo" | repeat 5
|
||||
"repeat": func(count int, str string) string { return strings.Repeat(str, count) },
|
||||
// Deprecated: Use trimAll.
|
||||
"trimall": func(a, b string) string { return strings.Trim(b, a) },
|
||||
// Switch order so that "$foo" | trimall "$"
|
||||
"trimAll": func(a, b string) string { return strings.Trim(b, a) },
|
||||
"trimSuffix": func(a, b string) string { return strings.TrimSuffix(b, a) },
|
||||
"trimPrefix": func(a, b string) string { return strings.TrimPrefix(b, a) },
|
||||
"nospace": util.DeleteWhiteSpace,
|
||||
"initials": initials,
|
||||
"randAlphaNum": randAlphaNumeric,
|
||||
"randAlpha": randAlpha,
|
||||
"randAscii": randAscii,
|
||||
"randNumeric": randNumeric,
|
||||
"swapcase": util.SwapCase,
|
||||
"shuffle": xstrings.Shuffle,
|
||||
"snakecase": xstrings.ToSnakeCase,
|
||||
"camelcase": xstrings.ToCamelCase,
|
||||
"kebabcase": xstrings.ToKebabCase,
|
||||
"wrap": func(l int, s string) string { return util.Wrap(s, l) },
|
||||
"wrapWith": func(l int, sep, str string) string { return util.WrapCustom(str, l, sep, true) },
|
||||
// Switch order so that "foobar" | contains "foo"
|
||||
"contains": func(substr string, str string) bool { return strings.Contains(str, substr) },
|
||||
"hasPrefix": func(substr string, str string) bool { return strings.HasPrefix(str, substr) },
|
||||
"hasSuffix": func(substr string, str string) bool { return strings.HasSuffix(str, substr) },
|
||||
"quote": quote,
|
||||
"squote": squote,
|
||||
"cat": cat,
|
||||
"indent": indent,
|
||||
"nindent": nindent,
|
||||
"replace": replace,
|
||||
"plural": plural,
|
||||
"sha1sum": sha1sum,
|
||||
"sha256sum": sha256sum,
|
||||
"adler32sum": adler32sum,
|
||||
"toString": strval,
|
||||
|
||||
// Wrap Atoi to stop errors.
|
||||
"atoi": func(a string) int { i, _ := strconv.Atoi(a); return i },
|
||||
"int64": toInt64,
|
||||
"int": toInt,
|
||||
"float64": toFloat64,
|
||||
"seq": seq,
|
||||
"toDecimal": toDecimal,
|
||||
|
||||
//"gt": func(a, b int) bool {return a > b},
|
||||
//"gte": func(a, b int) bool {return a >= b},
|
||||
//"lt": func(a, b int) bool {return a < b},
|
||||
//"lte": func(a, b int) bool {return a <= b},
|
||||
|
||||
// split "/" foo/bar returns map[int]string{0: foo, 1: bar}
|
||||
"split": split,
|
||||
"splitList": func(sep, orig string) []string { return strings.Split(orig, sep) },
|
||||
// splitn "/" foo/bar/fuu returns map[int]string{0: foo, 1: bar/fuu}
|
||||
"splitn": splitn,
|
||||
"toStrings": strslice,
|
||||
|
||||
"until": until,
|
||||
"untilStep": untilStep,
|
||||
|
||||
// VERY basic arithmetic.
|
||||
"add1": func(i interface{}) int64 { return toInt64(i) + 1 },
|
||||
"add": func(i ...interface{}) int64 {
|
||||
var a int64 = 0
|
||||
for _, b := range i {
|
||||
a += toInt64(b)
|
||||
}
|
||||
return a
|
||||
},
|
||||
"sub": func(a, b interface{}) int64 { return toInt64(a) - toInt64(b) },
|
||||
"div": func(a, b interface{}) int64 { return toInt64(a) / toInt64(b) },
|
||||
"mod": func(a, b interface{}) int64 { return toInt64(a) % toInt64(b) },
|
||||
"mul": func(a interface{}, v ...interface{}) int64 {
|
||||
val := toInt64(a)
|
||||
for _, b := range v {
|
||||
val = val * toInt64(b)
|
||||
}
|
||||
return val
|
||||
},
|
||||
"randInt": func(min, max int) int { return rand.Intn(max-min) + min },
|
||||
"add1f": func(i interface{}) float64 {
|
||||
return execDecimalOp(i, []interface{}{1}, func(d1, d2 decimal.Decimal) decimal.Decimal { return d1.Add(d2) })
|
||||
},
|
||||
"addf": func(i ...interface{}) float64 {
|
||||
a := interface{}(float64(0))
|
||||
return execDecimalOp(a, i, func(d1, d2 decimal.Decimal) decimal.Decimal { return d1.Add(d2) })
|
||||
},
|
||||
"subf": func(a interface{}, v ...interface{}) float64 {
|
||||
return execDecimalOp(a, v, func(d1, d2 decimal.Decimal) decimal.Decimal { return d1.Sub(d2) })
|
||||
},
|
||||
"divf": func(a interface{}, v ...interface{}) float64 {
|
||||
return execDecimalOp(a, v, func(d1, d2 decimal.Decimal) decimal.Decimal { return d1.Div(d2) })
|
||||
},
|
||||
"mulf": func(a interface{}, v ...interface{}) float64 {
|
||||
return execDecimalOp(a, v, func(d1, d2 decimal.Decimal) decimal.Decimal { return d1.Mul(d2) })
|
||||
},
|
||||
"biggest": max,
|
||||
"max": max,
|
||||
"min": min,
|
||||
"maxf": maxf,
|
||||
"minf": minf,
|
||||
"ceil": ceil,
|
||||
"floor": floor,
|
||||
"round": round,
|
||||
|
||||
// string slices. Note that we reverse the order b/c that's better
|
||||
// for template processing.
|
||||
"join": join,
|
||||
"sortAlpha": sortAlpha,
|
||||
|
||||
// Defaults
|
||||
"default": dfault,
|
||||
"empty": empty,
|
||||
"coalesce": coalesce,
|
||||
"all": all,
|
||||
"any": any,
|
||||
"compact": compact,
|
||||
"mustCompact": mustCompact,
|
||||
"fromJson": fromJson,
|
||||
"toJson": toJson,
|
||||
"toPrettyJson": toPrettyJson,
|
||||
"toRawJson": toRawJson,
|
||||
"mustFromJson": mustFromJson,
|
||||
"mustToJson": mustToJson,
|
||||
"mustToPrettyJson": mustToPrettyJson,
|
||||
"mustToRawJson": mustToRawJson,
|
||||
"ternary": ternary,
|
||||
"deepCopy": deepCopy,
|
||||
"mustDeepCopy": mustDeepCopy,
|
||||
|
||||
// Reflection
|
||||
"typeOf": typeOf,
|
||||
"typeIs": typeIs,
|
||||
"typeIsLike": typeIsLike,
|
||||
"kindOf": kindOf,
|
||||
"kindIs": kindIs,
|
||||
"deepEqual": reflect.DeepEqual,
|
||||
|
||||
// OS:
|
||||
"env": os.Getenv,
|
||||
"expandenv": os.ExpandEnv,
|
||||
|
||||
// Network:
|
||||
"getHostByName": getHostByName,
|
||||
|
||||
// Paths:
|
||||
"base": path.Base,
|
||||
"dir": path.Dir,
|
||||
"clean": path.Clean,
|
||||
"ext": path.Ext,
|
||||
"isAbs": path.IsAbs,
|
||||
|
||||
// Filepaths:
|
||||
"osBase": filepath.Base,
|
||||
"osClean": filepath.Clean,
|
||||
"osDir": filepath.Dir,
|
||||
"osExt": filepath.Ext,
|
||||
"osIsAbs": filepath.IsAbs,
|
||||
|
||||
// Encoding:
|
||||
"b64enc": base64encode,
|
||||
"b64dec": base64decode,
|
||||
"b32enc": base32encode,
|
||||
"b32dec": base32decode,
|
||||
|
||||
// Data Structures:
|
||||
"tuple": list, // FIXME: with the addition of append/prepend these are no longer immutable.
|
||||
"list": list,
|
||||
"dict": dict,
|
||||
"get": get,
|
||||
"set": set,
|
||||
"unset": unset,
|
||||
"hasKey": hasKey,
|
||||
"pluck": pluck,
|
||||
"keys": keys,
|
||||
"pick": pick,
|
||||
"omit": omit,
|
||||
"merge": merge,
|
||||
"mergeOverwrite": mergeOverwrite,
|
||||
"mustMerge": mustMerge,
|
||||
"mustMergeOverwrite": mustMergeOverwrite,
|
||||
"values": values,
|
||||
|
||||
"append": push, "push": push,
|
||||
"mustAppend": mustPush, "mustPush": mustPush,
|
||||
"prepend": prepend,
|
||||
"mustPrepend": mustPrepend,
|
||||
"first": first,
|
||||
"mustFirst": mustFirst,
|
||||
"rest": rest,
|
||||
"mustRest": mustRest,
|
||||
"last": last,
|
||||
"mustLast": mustLast,
|
||||
"initial": initial,
|
||||
"mustInitial": mustInitial,
|
||||
"reverse": reverse,
|
||||
"mustReverse": mustReverse,
|
||||
"uniq": uniq,
|
||||
"mustUniq": mustUniq,
|
||||
"without": without,
|
||||
"mustWithout": mustWithout,
|
||||
"has": has,
|
||||
"mustHas": mustHas,
|
||||
"slice": slice,
|
||||
"mustSlice": mustSlice,
|
||||
"concat": concat,
|
||||
"dig": dig,
|
||||
"chunk": chunk,
|
||||
"mustChunk": mustChunk,
|
||||
|
||||
// Crypto:
|
||||
"bcrypt": bcrypt,
|
||||
"htpasswd": htpasswd,
|
||||
"genPrivateKey": generatePrivateKey,
|
||||
"derivePassword": derivePassword,
|
||||
"buildCustomCert": buildCustomCertificate,
|
||||
"genCA": generateCertificateAuthority,
|
||||
"genCAWithKey": generateCertificateAuthorityWithPEMKey,
|
||||
"genSelfSignedCert": generateSelfSignedCertificate,
|
||||
"genSelfSignedCertWithKey": generateSelfSignedCertificateWithPEMKey,
|
||||
"genSignedCert": generateSignedCertificate,
|
||||
"genSignedCertWithKey": generateSignedCertificateWithPEMKey,
|
||||
"encryptAES": encryptAES,
|
||||
"decryptAES": decryptAES,
|
||||
"randBytes": randBytes,
|
||||
|
||||
// UUIDs:
|
||||
"uuidv4": uuidv4,
|
||||
|
||||
// SemVer:
|
||||
"semver": semver,
|
||||
"semverCompare": semverCompare,
|
||||
|
||||
// Flow Control:
|
||||
"fail": func(msg string) (string, error) { return "", errors.New(msg) },
|
||||
|
||||
// Regex
|
||||
"regexMatch": regexMatch,
|
||||
"mustRegexMatch": mustRegexMatch,
|
||||
"regexFindAll": regexFindAll,
|
||||
"mustRegexFindAll": mustRegexFindAll,
|
||||
"regexFind": regexFind,
|
||||
"mustRegexFind": mustRegexFind,
|
||||
"regexReplaceAll": regexReplaceAll,
|
||||
"mustRegexReplaceAll": mustRegexReplaceAll,
|
||||
"regexReplaceAllLiteral": regexReplaceAllLiteral,
|
||||
"mustRegexReplaceAllLiteral": mustRegexReplaceAllLiteral,
|
||||
"regexSplit": regexSplit,
|
||||
"mustRegexSplit": mustRegexSplit,
|
||||
"regexQuoteMeta": regexQuoteMeta,
|
||||
|
||||
// URLs:
|
||||
"urlParse": urlParse,
|
||||
"urlJoin": urlJoin,
|
||||
}
|
||||
464
vendor/github.com/Masterminds/sprig/v3/list.go
generated
vendored
Normal file
464
vendor/github.com/Masterminds/sprig/v3/list.go
generated
vendored
Normal file
@@ -0,0 +1,464 @@
|
||||
package sprig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"reflect"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// Reflection is used in these functions so that slices and arrays of strings,
|
||||
// ints, and other types not implementing []interface{} can be worked with.
|
||||
// For example, this is useful if you need to work on the output of regexs.
|
||||
|
||||
func list(v ...interface{}) []interface{} {
|
||||
return v
|
||||
}
|
||||
|
||||
func push(list interface{}, v interface{}) []interface{} {
|
||||
l, err := mustPush(list, v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return l
|
||||
}
|
||||
|
||||
func mustPush(list interface{}, v interface{}) ([]interface{}, error) {
|
||||
tp := reflect.TypeOf(list).Kind()
|
||||
switch tp {
|
||||
case reflect.Slice, reflect.Array:
|
||||
l2 := reflect.ValueOf(list)
|
||||
|
||||
l := l2.Len()
|
||||
nl := make([]interface{}, l)
|
||||
for i := 0; i < l; i++ {
|
||||
nl[i] = l2.Index(i).Interface()
|
||||
}
|
||||
|
||||
return append(nl, v), nil
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("Cannot push on type %s", tp)
|
||||
}
|
||||
}
|
||||
|
||||
func prepend(list interface{}, v interface{}) []interface{} {
|
||||
l, err := mustPrepend(list, v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return l
|
||||
}
|
||||
|
||||
func mustPrepend(list interface{}, v interface{}) ([]interface{}, error) {
|
||||
//return append([]interface{}{v}, list...)
|
||||
|
||||
tp := reflect.TypeOf(list).Kind()
|
||||
switch tp {
|
||||
case reflect.Slice, reflect.Array:
|
||||
l2 := reflect.ValueOf(list)
|
||||
|
||||
l := l2.Len()
|
||||
nl := make([]interface{}, l)
|
||||
for i := 0; i < l; i++ {
|
||||
nl[i] = l2.Index(i).Interface()
|
||||
}
|
||||
|
||||
return append([]interface{}{v}, nl...), nil
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("Cannot prepend on type %s", tp)
|
||||
}
|
||||
}
|
||||
|
||||
func chunk(size int, list interface{}) [][]interface{} {
|
||||
l, err := mustChunk(size, list)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return l
|
||||
}
|
||||
|
||||
func mustChunk(size int, list interface{}) ([][]interface{}, error) {
|
||||
tp := reflect.TypeOf(list).Kind()
|
||||
switch tp {
|
||||
case reflect.Slice, reflect.Array:
|
||||
l2 := reflect.ValueOf(list)
|
||||
|
||||
l := l2.Len()
|
||||
|
||||
cs := int(math.Floor(float64(l-1)/float64(size)) + 1)
|
||||
nl := make([][]interface{}, cs)
|
||||
|
||||
for i := 0; i < cs; i++ {
|
||||
clen := size
|
||||
if i == cs-1 {
|
||||
clen = int(math.Floor(math.Mod(float64(l), float64(size))))
|
||||
if clen == 0 {
|
||||
clen = size
|
||||
}
|
||||
}
|
||||
|
||||
nl[i] = make([]interface{}, clen)
|
||||
|
||||
for j := 0; j < clen; j++ {
|
||||
ix := i*size + j
|
||||
nl[i][j] = l2.Index(ix).Interface()
|
||||
}
|
||||
}
|
||||
|
||||
return nl, nil
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("Cannot chunk type %s", tp)
|
||||
}
|
||||
}
|
||||
|
||||
func last(list interface{}) interface{} {
|
||||
l, err := mustLast(list)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return l
|
||||
}
|
||||
|
||||
func mustLast(list interface{}) (interface{}, error) {
|
||||
tp := reflect.TypeOf(list).Kind()
|
||||
switch tp {
|
||||
case reflect.Slice, reflect.Array:
|
||||
l2 := reflect.ValueOf(list)
|
||||
|
||||
l := l2.Len()
|
||||
if l == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return l2.Index(l - 1).Interface(), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("Cannot find last on type %s", tp)
|
||||
}
|
||||
}
|
||||
|
||||
func first(list interface{}) interface{} {
|
||||
l, err := mustFirst(list)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return l
|
||||
}
|
||||
|
||||
func mustFirst(list interface{}) (interface{}, error) {
|
||||
tp := reflect.TypeOf(list).Kind()
|
||||
switch tp {
|
||||
case reflect.Slice, reflect.Array:
|
||||
l2 := reflect.ValueOf(list)
|
||||
|
||||
l := l2.Len()
|
||||
if l == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return l2.Index(0).Interface(), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("Cannot find first on type %s", tp)
|
||||
}
|
||||
}
|
||||
|
||||
func rest(list interface{}) []interface{} {
|
||||
l, err := mustRest(list)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return l
|
||||
}
|
||||
|
||||
func mustRest(list interface{}) ([]interface{}, error) {
|
||||
tp := reflect.TypeOf(list).Kind()
|
||||
switch tp {
|
||||
case reflect.Slice, reflect.Array:
|
||||
l2 := reflect.ValueOf(list)
|
||||
|
||||
l := l2.Len()
|
||||
if l == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
nl := make([]interface{}, l-1)
|
||||
for i := 1; i < l; i++ {
|
||||
nl[i-1] = l2.Index(i).Interface()
|
||||
}
|
||||
|
||||
return nl, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("Cannot find rest on type %s", tp)
|
||||
}
|
||||
}
|
||||
|
||||
func initial(list interface{}) []interface{} {
|
||||
l, err := mustInitial(list)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return l
|
||||
}
|
||||
|
||||
func mustInitial(list interface{}) ([]interface{}, error) {
|
||||
tp := reflect.TypeOf(list).Kind()
|
||||
switch tp {
|
||||
case reflect.Slice, reflect.Array:
|
||||
l2 := reflect.ValueOf(list)
|
||||
|
||||
l := l2.Len()
|
||||
if l == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
nl := make([]interface{}, l-1)
|
||||
for i := 0; i < l-1; i++ {
|
||||
nl[i] = l2.Index(i).Interface()
|
||||
}
|
||||
|
||||
return nl, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("Cannot find initial on type %s", tp)
|
||||
}
|
||||
}
|
||||
|
||||
func sortAlpha(list interface{}) []string {
|
||||
k := reflect.Indirect(reflect.ValueOf(list)).Kind()
|
||||
switch k {
|
||||
case reflect.Slice, reflect.Array:
|
||||
a := strslice(list)
|
||||
s := sort.StringSlice(a)
|
||||
s.Sort()
|
||||
return s
|
||||
}
|
||||
return []string{strval(list)}
|
||||
}
|
||||
|
||||
func reverse(v interface{}) []interface{} {
|
||||
l, err := mustReverse(v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return l
|
||||
}
|
||||
|
||||
func mustReverse(v interface{}) ([]interface{}, error) {
|
||||
tp := reflect.TypeOf(v).Kind()
|
||||
switch tp {
|
||||
case reflect.Slice, reflect.Array:
|
||||
l2 := reflect.ValueOf(v)
|
||||
|
||||
l := l2.Len()
|
||||
// We do not sort in place because the incoming array should not be altered.
|
||||
nl := make([]interface{}, l)
|
||||
for i := 0; i < l; i++ {
|
||||
nl[l-i-1] = l2.Index(i).Interface()
|
||||
}
|
||||
|
||||
return nl, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("Cannot find reverse on type %s", tp)
|
||||
}
|
||||
}
|
||||
|
||||
func compact(list interface{}) []interface{} {
|
||||
l, err := mustCompact(list)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return l
|
||||
}
|
||||
|
||||
func mustCompact(list interface{}) ([]interface{}, error) {
|
||||
tp := reflect.TypeOf(list).Kind()
|
||||
switch tp {
|
||||
case reflect.Slice, reflect.Array:
|
||||
l2 := reflect.ValueOf(list)
|
||||
|
||||
l := l2.Len()
|
||||
nl := []interface{}{}
|
||||
var item interface{}
|
||||
for i := 0; i < l; i++ {
|
||||
item = l2.Index(i).Interface()
|
||||
if !empty(item) {
|
||||
nl = append(nl, item)
|
||||
}
|
||||
}
|
||||
|
||||
return nl, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("Cannot compact on type %s", tp)
|
||||
}
|
||||
}
|
||||
|
||||
func uniq(list interface{}) []interface{} {
|
||||
l, err := mustUniq(list)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return l
|
||||
}
|
||||
|
||||
func mustUniq(list interface{}) ([]interface{}, error) {
|
||||
tp := reflect.TypeOf(list).Kind()
|
||||
switch tp {
|
||||
case reflect.Slice, reflect.Array:
|
||||
l2 := reflect.ValueOf(list)
|
||||
|
||||
l := l2.Len()
|
||||
dest := []interface{}{}
|
||||
var item interface{}
|
||||
for i := 0; i < l; i++ {
|
||||
item = l2.Index(i).Interface()
|
||||
if !inList(dest, item) {
|
||||
dest = append(dest, item)
|
||||
}
|
||||
}
|
||||
|
||||
return dest, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("Cannot find uniq on type %s", tp)
|
||||
}
|
||||
}
|
||||
|
||||
func inList(haystack []interface{}, needle interface{}) bool {
|
||||
for _, h := range haystack {
|
||||
if reflect.DeepEqual(needle, h) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func without(list interface{}, omit ...interface{}) []interface{} {
|
||||
l, err := mustWithout(list, omit...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return l
|
||||
}
|
||||
|
||||
func mustWithout(list interface{}, omit ...interface{}) ([]interface{}, error) {
|
||||
tp := reflect.TypeOf(list).Kind()
|
||||
switch tp {
|
||||
case reflect.Slice, reflect.Array:
|
||||
l2 := reflect.ValueOf(list)
|
||||
|
||||
l := l2.Len()
|
||||
res := []interface{}{}
|
||||
var item interface{}
|
||||
for i := 0; i < l; i++ {
|
||||
item = l2.Index(i).Interface()
|
||||
if !inList(omit, item) {
|
||||
res = append(res, item)
|
||||
}
|
||||
}
|
||||
|
||||
return res, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("Cannot find without on type %s", tp)
|
||||
}
|
||||
}
|
||||
|
||||
func has(needle interface{}, haystack interface{}) bool {
|
||||
l, err := mustHas(needle, haystack)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return l
|
||||
}
|
||||
|
||||
func mustHas(needle interface{}, haystack interface{}) (bool, error) {
|
||||
if haystack == nil {
|
||||
return false, nil
|
||||
}
|
||||
tp := reflect.TypeOf(haystack).Kind()
|
||||
switch tp {
|
||||
case reflect.Slice, reflect.Array:
|
||||
l2 := reflect.ValueOf(haystack)
|
||||
var item interface{}
|
||||
l := l2.Len()
|
||||
for i := 0; i < l; i++ {
|
||||
item = l2.Index(i).Interface()
|
||||
if reflect.DeepEqual(needle, item) {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
default:
|
||||
return false, fmt.Errorf("Cannot find has on type %s", tp)
|
||||
}
|
||||
}
|
||||
|
||||
// $list := [1, 2, 3, 4, 5]
|
||||
// slice $list -> list[0:5] = list[:]
|
||||
// slice $list 0 3 -> list[0:3] = list[:3]
|
||||
// slice $list 3 5 -> list[3:5]
|
||||
// slice $list 3 -> list[3:5] = list[3:]
|
||||
func slice(list interface{}, indices ...interface{}) interface{} {
|
||||
l, err := mustSlice(list, indices...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return l
|
||||
}
|
||||
|
||||
func mustSlice(list interface{}, indices ...interface{}) (interface{}, error) {
|
||||
tp := reflect.TypeOf(list).Kind()
|
||||
switch tp {
|
||||
case reflect.Slice, reflect.Array:
|
||||
l2 := reflect.ValueOf(list)
|
||||
|
||||
l := l2.Len()
|
||||
if l == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var start, end int
|
||||
if len(indices) > 0 {
|
||||
start = toInt(indices[0])
|
||||
}
|
||||
if len(indices) < 2 {
|
||||
end = l
|
||||
} else {
|
||||
end = toInt(indices[1])
|
||||
}
|
||||
|
||||
return l2.Slice(start, end).Interface(), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("list should be type of slice or array but %s", tp)
|
||||
}
|
||||
}
|
||||
|
||||
func concat(lists ...interface{}) interface{} {
|
||||
var res []interface{}
|
||||
for _, list := range lists {
|
||||
tp := reflect.TypeOf(list).Kind()
|
||||
switch tp {
|
||||
case reflect.Slice, reflect.Array:
|
||||
l2 := reflect.ValueOf(list)
|
||||
for i := 0; i < l2.Len(); i++ {
|
||||
res = append(res, l2.Index(i).Interface())
|
||||
}
|
||||
default:
|
||||
panic(fmt.Sprintf("Cannot concat type %s as list", tp))
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
12
vendor/github.com/Masterminds/sprig/v3/network.go
generated
vendored
Normal file
12
vendor/github.com/Masterminds/sprig/v3/network.go
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
package sprig
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"net"
|
||||
)
|
||||
|
||||
func getHostByName(name string) string {
|
||||
addrs, _ := net.LookupHost(name)
|
||||
//TODO: add error handing when release v3 comes out
|
||||
return addrs[rand.Intn(len(addrs))]
|
||||
}
|
||||
186
vendor/github.com/Masterminds/sprig/v3/numeric.go
generated
vendored
Normal file
186
vendor/github.com/Masterminds/sprig/v3/numeric.go
generated
vendored
Normal file
@@ -0,0 +1,186 @@
|
||||
package sprig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
// toFloat64 converts 64-bit floats
|
||||
func toFloat64(v interface{}) float64 {
|
||||
return cast.ToFloat64(v)
|
||||
}
|
||||
|
||||
func toInt(v interface{}) int {
|
||||
return cast.ToInt(v)
|
||||
}
|
||||
|
||||
// toInt64 converts integer types to 64-bit integers
|
||||
func toInt64(v interface{}) int64 {
|
||||
return cast.ToInt64(v)
|
||||
}
|
||||
|
||||
func max(a interface{}, i ...interface{}) int64 {
|
||||
aa := toInt64(a)
|
||||
for _, b := range i {
|
||||
bb := toInt64(b)
|
||||
if bb > aa {
|
||||
aa = bb
|
||||
}
|
||||
}
|
||||
return aa
|
||||
}
|
||||
|
||||
func maxf(a interface{}, i ...interface{}) float64 {
|
||||
aa := toFloat64(a)
|
||||
for _, b := range i {
|
||||
bb := toFloat64(b)
|
||||
aa = math.Max(aa, bb)
|
||||
}
|
||||
return aa
|
||||
}
|
||||
|
||||
func min(a interface{}, i ...interface{}) int64 {
|
||||
aa := toInt64(a)
|
||||
for _, b := range i {
|
||||
bb := toInt64(b)
|
||||
if bb < aa {
|
||||
aa = bb
|
||||
}
|
||||
}
|
||||
return aa
|
||||
}
|
||||
|
||||
func minf(a interface{}, i ...interface{}) float64 {
|
||||
aa := toFloat64(a)
|
||||
for _, b := range i {
|
||||
bb := toFloat64(b)
|
||||
aa = math.Min(aa, bb)
|
||||
}
|
||||
return aa
|
||||
}
|
||||
|
||||
func until(count int) []int {
|
||||
step := 1
|
||||
if count < 0 {
|
||||
step = -1
|
||||
}
|
||||
return untilStep(0, count, step)
|
||||
}
|
||||
|
||||
func untilStep(start, stop, step int) []int {
|
||||
v := []int{}
|
||||
|
||||
if stop < start {
|
||||
if step >= 0 {
|
||||
return v
|
||||
}
|
||||
for i := start; i > stop; i += step {
|
||||
v = append(v, i)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
if step <= 0 {
|
||||
return v
|
||||
}
|
||||
for i := start; i < stop; i += step {
|
||||
v = append(v, i)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func floor(a interface{}) float64 {
|
||||
aa := toFloat64(a)
|
||||
return math.Floor(aa)
|
||||
}
|
||||
|
||||
func ceil(a interface{}) float64 {
|
||||
aa := toFloat64(a)
|
||||
return math.Ceil(aa)
|
||||
}
|
||||
|
||||
func round(a interface{}, p int, rOpt ...float64) float64 {
|
||||
roundOn := .5
|
||||
if len(rOpt) > 0 {
|
||||
roundOn = rOpt[0]
|
||||
}
|
||||
val := toFloat64(a)
|
||||
places := toFloat64(p)
|
||||
|
||||
var round float64
|
||||
pow := math.Pow(10, places)
|
||||
digit := pow * val
|
||||
_, div := math.Modf(digit)
|
||||
if div >= roundOn {
|
||||
round = math.Ceil(digit)
|
||||
} else {
|
||||
round = math.Floor(digit)
|
||||
}
|
||||
return round / pow
|
||||
}
|
||||
|
||||
// converts unix octal to decimal
|
||||
func toDecimal(v interface{}) int64 {
|
||||
result, err := strconv.ParseInt(fmt.Sprint(v), 8, 64)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func seq(params ...int) string {
|
||||
increment := 1
|
||||
switch len(params) {
|
||||
case 0:
|
||||
return ""
|
||||
case 1:
|
||||
start := 1
|
||||
end := params[0]
|
||||
if end < start {
|
||||
increment = -1
|
||||
}
|
||||
return intArrayToString(untilStep(start, end+increment, increment), " ")
|
||||
case 3:
|
||||
start := params[0]
|
||||
end := params[2]
|
||||
step := params[1]
|
||||
if end < start {
|
||||
increment = -1
|
||||
if step > 0 {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
return intArrayToString(untilStep(start, end+increment, step), " ")
|
||||
case 2:
|
||||
start := params[0]
|
||||
end := params[1]
|
||||
step := 1
|
||||
if end < start {
|
||||
step = -1
|
||||
}
|
||||
return intArrayToString(untilStep(start, end+step, step), " ")
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func intArrayToString(slice []int, delimeter string) string {
|
||||
return strings.Trim(strings.Join(strings.Fields(fmt.Sprint(slice)), delimeter), "[]")
|
||||
}
|
||||
|
||||
// performs a float and subsequent decimal.Decimal conversion on inputs,
|
||||
// and iterates through a and b executing the mathmetical operation f
|
||||
func execDecimalOp(a interface{}, b []interface{}, f func(d1, d2 decimal.Decimal) decimal.Decimal) float64 {
|
||||
prt := decimal.NewFromFloat(toFloat64(a))
|
||||
for _, x := range b {
|
||||
dx := decimal.NewFromFloat(toFloat64(x))
|
||||
prt = f(prt, dx)
|
||||
}
|
||||
rslt, _ := prt.Float64()
|
||||
return rslt
|
||||
}
|
||||
28
vendor/github.com/Masterminds/sprig/v3/reflect.go
generated
vendored
Normal file
28
vendor/github.com/Masterminds/sprig/v3/reflect.go
generated
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
package sprig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// typeIs returns true if the src is the type named in target.
|
||||
func typeIs(target string, src interface{}) bool {
|
||||
return target == typeOf(src)
|
||||
}
|
||||
|
||||
func typeIsLike(target string, src interface{}) bool {
|
||||
t := typeOf(src)
|
||||
return target == t || "*"+target == t
|
||||
}
|
||||
|
||||
func typeOf(src interface{}) string {
|
||||
return fmt.Sprintf("%T", src)
|
||||
}
|
||||
|
||||
func kindIs(target string, src interface{}) bool {
|
||||
return target == kindOf(src)
|
||||
}
|
||||
|
||||
func kindOf(src interface{}) string {
|
||||
return reflect.ValueOf(src).Kind().String()
|
||||
}
|
||||
83
vendor/github.com/Masterminds/sprig/v3/regex.go
generated
vendored
Normal file
83
vendor/github.com/Masterminds/sprig/v3/regex.go
generated
vendored
Normal file
@@ -0,0 +1,83 @@
|
||||
package sprig
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func regexMatch(regex string, s string) bool {
|
||||
match, _ := regexp.MatchString(regex, s)
|
||||
return match
|
||||
}
|
||||
|
||||
func mustRegexMatch(regex string, s string) (bool, error) {
|
||||
return regexp.MatchString(regex, s)
|
||||
}
|
||||
|
||||
func regexFindAll(regex string, s string, n int) []string {
|
||||
r := regexp.MustCompile(regex)
|
||||
return r.FindAllString(s, n)
|
||||
}
|
||||
|
||||
func mustRegexFindAll(regex string, s string, n int) ([]string, error) {
|
||||
r, err := regexp.Compile(regex)
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
return r.FindAllString(s, n), nil
|
||||
}
|
||||
|
||||
func regexFind(regex string, s string) string {
|
||||
r := regexp.MustCompile(regex)
|
||||
return r.FindString(s)
|
||||
}
|
||||
|
||||
func mustRegexFind(regex string, s string) (string, error) {
|
||||
r, err := regexp.Compile(regex)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return r.FindString(s), nil
|
||||
}
|
||||
|
||||
func regexReplaceAll(regex string, s string, repl string) string {
|
||||
r := regexp.MustCompile(regex)
|
||||
return r.ReplaceAllString(s, repl)
|
||||
}
|
||||
|
||||
func mustRegexReplaceAll(regex string, s string, repl string) (string, error) {
|
||||
r, err := regexp.Compile(regex)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return r.ReplaceAllString(s, repl), nil
|
||||
}
|
||||
|
||||
func regexReplaceAllLiteral(regex string, s string, repl string) string {
|
||||
r := regexp.MustCompile(regex)
|
||||
return r.ReplaceAllLiteralString(s, repl)
|
||||
}
|
||||
|
||||
func mustRegexReplaceAllLiteral(regex string, s string, repl string) (string, error) {
|
||||
r, err := regexp.Compile(regex)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return r.ReplaceAllLiteralString(s, repl), nil
|
||||
}
|
||||
|
||||
func regexSplit(regex string, s string, n int) []string {
|
||||
r := regexp.MustCompile(regex)
|
||||
return r.Split(s, n)
|
||||
}
|
||||
|
||||
func mustRegexSplit(regex string, s string, n int) ([]string, error) {
|
||||
r, err := regexp.Compile(regex)
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
return r.Split(s, n), nil
|
||||
}
|
||||
|
||||
func regexQuoteMeta(s string) string {
|
||||
return regexp.QuoteMeta(s)
|
||||
}
|
||||
23
vendor/github.com/Masterminds/sprig/v3/semver.go
generated
vendored
Normal file
23
vendor/github.com/Masterminds/sprig/v3/semver.go
generated
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
package sprig
|
||||
|
||||
import (
|
||||
sv2 "github.com/Masterminds/semver/v3"
|
||||
)
|
||||
|
||||
func semverCompare(constraint, version string) (bool, error) {
|
||||
c, err := sv2.NewConstraint(constraint)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
v, err := sv2.NewVersion(version)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return c.Check(v), nil
|
||||
}
|
||||
|
||||
func semver(version string) (*sv2.Version, error) {
|
||||
return sv2.NewVersion(version)
|
||||
}
|
||||
236
vendor/github.com/Masterminds/sprig/v3/strings.go
generated
vendored
Normal file
236
vendor/github.com/Masterminds/sprig/v3/strings.go
generated
vendored
Normal file
@@ -0,0 +1,236 @@
|
||||
package sprig
|
||||
|
||||
import (
|
||||
"encoding/base32"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
util "github.com/Masterminds/goutils"
|
||||
)
|
||||
|
||||
func base64encode(v string) string {
|
||||
return base64.StdEncoding.EncodeToString([]byte(v))
|
||||
}
|
||||
|
||||
func base64decode(v string) string {
|
||||
data, err := base64.StdEncoding.DecodeString(v)
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func base32encode(v string) string {
|
||||
return base32.StdEncoding.EncodeToString([]byte(v))
|
||||
}
|
||||
|
||||
func base32decode(v string) string {
|
||||
data, err := base32.StdEncoding.DecodeString(v)
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func abbrev(width int, s string) string {
|
||||
if width < 4 {
|
||||
return s
|
||||
}
|
||||
r, _ := util.Abbreviate(s, width)
|
||||
return r
|
||||
}
|
||||
|
||||
func abbrevboth(left, right int, s string) string {
|
||||
if right < 4 || left > 0 && right < 7 {
|
||||
return s
|
||||
}
|
||||
r, _ := util.AbbreviateFull(s, left, right)
|
||||
return r
|
||||
}
|
||||
func initials(s string) string {
|
||||
// Wrap this just to eliminate the var args, which templates don't do well.
|
||||
return util.Initials(s)
|
||||
}
|
||||
|
||||
func randAlphaNumeric(count int) string {
|
||||
// It is not possible, it appears, to actually generate an error here.
|
||||
r, _ := util.CryptoRandomAlphaNumeric(count)
|
||||
return r
|
||||
}
|
||||
|
||||
func randAlpha(count int) string {
|
||||
r, _ := util.CryptoRandomAlphabetic(count)
|
||||
return r
|
||||
}
|
||||
|
||||
func randAscii(count int) string {
|
||||
r, _ := util.CryptoRandomAscii(count)
|
||||
return r
|
||||
}
|
||||
|
||||
func randNumeric(count int) string {
|
||||
r, _ := util.CryptoRandomNumeric(count)
|
||||
return r
|
||||
}
|
||||
|
||||
func untitle(str string) string {
|
||||
return util.Uncapitalize(str)
|
||||
}
|
||||
|
||||
func quote(str ...interface{}) string {
|
||||
out := make([]string, 0, len(str))
|
||||
for _, s := range str {
|
||||
if s != nil {
|
||||
out = append(out, fmt.Sprintf("%q", strval(s)))
|
||||
}
|
||||
}
|
||||
return strings.Join(out, " ")
|
||||
}
|
||||
|
||||
func squote(str ...interface{}) string {
|
||||
out := make([]string, 0, len(str))
|
||||
for _, s := range str {
|
||||
if s != nil {
|
||||
out = append(out, fmt.Sprintf("'%v'", s))
|
||||
}
|
||||
}
|
||||
return strings.Join(out, " ")
|
||||
}
|
||||
|
||||
func cat(v ...interface{}) string {
|
||||
v = removeNilElements(v)
|
||||
r := strings.TrimSpace(strings.Repeat("%v ", len(v)))
|
||||
return fmt.Sprintf(r, v...)
|
||||
}
|
||||
|
||||
func indent(spaces int, v string) string {
|
||||
pad := strings.Repeat(" ", spaces)
|
||||
return pad + strings.Replace(v, "\n", "\n"+pad, -1)
|
||||
}
|
||||
|
||||
func nindent(spaces int, v string) string {
|
||||
return "\n" + indent(spaces, v)
|
||||
}
|
||||
|
||||
func replace(old, new, src string) string {
|
||||
return strings.Replace(src, old, new, -1)
|
||||
}
|
||||
|
||||
func plural(one, many string, count int) string {
|
||||
if count == 1 {
|
||||
return one
|
||||
}
|
||||
return many
|
||||
}
|
||||
|
||||
func strslice(v interface{}) []string {
|
||||
switch v := v.(type) {
|
||||
case []string:
|
||||
return v
|
||||
case []interface{}:
|
||||
b := make([]string, 0, len(v))
|
||||
for _, s := range v {
|
||||
if s != nil {
|
||||
b = append(b, strval(s))
|
||||
}
|
||||
}
|
||||
return b
|
||||
default:
|
||||
val := reflect.ValueOf(v)
|
||||
switch val.Kind() {
|
||||
case reflect.Array, reflect.Slice:
|
||||
l := val.Len()
|
||||
b := make([]string, 0, l)
|
||||
for i := 0; i < l; i++ {
|
||||
value := val.Index(i).Interface()
|
||||
if value != nil {
|
||||
b = append(b, strval(value))
|
||||
}
|
||||
}
|
||||
return b
|
||||
default:
|
||||
if v == nil {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
return []string{strval(v)}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func removeNilElements(v []interface{}) []interface{} {
|
||||
newSlice := make([]interface{}, 0, len(v))
|
||||
for _, i := range v {
|
||||
if i != nil {
|
||||
newSlice = append(newSlice, i)
|
||||
}
|
||||
}
|
||||
return newSlice
|
||||
}
|
||||
|
||||
func strval(v interface{}) string {
|
||||
switch v := v.(type) {
|
||||
case string:
|
||||
return v
|
||||
case []byte:
|
||||
return string(v)
|
||||
case error:
|
||||
return v.Error()
|
||||
case fmt.Stringer:
|
||||
return v.String()
|
||||
default:
|
||||
return fmt.Sprintf("%v", v)
|
||||
}
|
||||
}
|
||||
|
||||
func trunc(c int, s string) string {
|
||||
if c < 0 && len(s)+c > 0 {
|
||||
return s[len(s)+c:]
|
||||
}
|
||||
if c >= 0 && len(s) > c {
|
||||
return s[:c]
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func join(sep string, v interface{}) string {
|
||||
return strings.Join(strslice(v), sep)
|
||||
}
|
||||
|
||||
func split(sep, orig string) map[string]string {
|
||||
parts := strings.Split(orig, sep)
|
||||
res := make(map[string]string, len(parts))
|
||||
for i, v := range parts {
|
||||
res["_"+strconv.Itoa(i)] = v
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func splitn(sep string, n int, orig string) map[string]string {
|
||||
parts := strings.SplitN(orig, sep, n)
|
||||
res := make(map[string]string, len(parts))
|
||||
for i, v := range parts {
|
||||
res["_"+strconv.Itoa(i)] = v
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// substring creates a substring of the given string.
|
||||
//
|
||||
// If start is < 0, this calls string[:end].
|
||||
//
|
||||
// If start is >= 0 and end < 0 or end bigger than s length, this calls string[start:]
|
||||
//
|
||||
// Otherwise, this calls string[start, end].
|
||||
func substring(start, end int, s string) string {
|
||||
if start < 0 {
|
||||
return s[:end]
|
||||
}
|
||||
if end < 0 || end > len(s) {
|
||||
return s[start:]
|
||||
}
|
||||
return s[start:end]
|
||||
}
|
||||
66
vendor/github.com/Masterminds/sprig/v3/url.go
generated
vendored
Normal file
66
vendor/github.com/Masterminds/sprig/v3/url.go
generated
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
package sprig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func dictGetOrEmpty(dict map[string]interface{}, key string) string {
|
||||
value, ok := dict[key]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
tp := reflect.TypeOf(value).Kind()
|
||||
if tp != reflect.String {
|
||||
panic(fmt.Sprintf("unable to parse %s key, must be of type string, but %s found", key, tp.String()))
|
||||
}
|
||||
return reflect.ValueOf(value).String()
|
||||
}
|
||||
|
||||
// parses given URL to return dict object
|
||||
func urlParse(v string) map[string]interface{} {
|
||||
dict := map[string]interface{}{}
|
||||
parsedURL, err := url.Parse(v)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("unable to parse url: %s", err))
|
||||
}
|
||||
dict["scheme"] = parsedURL.Scheme
|
||||
dict["host"] = parsedURL.Host
|
||||
dict["hostname"] = parsedURL.Hostname()
|
||||
dict["path"] = parsedURL.Path
|
||||
dict["query"] = parsedURL.RawQuery
|
||||
dict["opaque"] = parsedURL.Opaque
|
||||
dict["fragment"] = parsedURL.Fragment
|
||||
if parsedURL.User != nil {
|
||||
dict["userinfo"] = parsedURL.User.String()
|
||||
} else {
|
||||
dict["userinfo"] = ""
|
||||
}
|
||||
|
||||
return dict
|
||||
}
|
||||
|
||||
// join given dict to URL string
|
||||
func urlJoin(d map[string]interface{}) string {
|
||||
resURL := url.URL{
|
||||
Scheme: dictGetOrEmpty(d, "scheme"),
|
||||
Host: dictGetOrEmpty(d, "host"),
|
||||
Path: dictGetOrEmpty(d, "path"),
|
||||
RawQuery: dictGetOrEmpty(d, "query"),
|
||||
Opaque: dictGetOrEmpty(d, "opaque"),
|
||||
Fragment: dictGetOrEmpty(d, "fragment"),
|
||||
}
|
||||
userinfo := dictGetOrEmpty(d, "userinfo")
|
||||
var user *url.Userinfo
|
||||
if userinfo != "" {
|
||||
tempURL, err := url.Parse(fmt.Sprintf("proto://%s@host", userinfo))
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("unable to parse userinfo in dict: %s", err))
|
||||
}
|
||||
user = tempURL.User
|
||||
}
|
||||
|
||||
resURL.User = user
|
||||
return resURL.String()
|
||||
}
|
||||
15
vendor/github.com/asaskevich/govalidator/.gitignore
generated
vendored
Normal file
15
vendor/github.com/asaskevich/govalidator/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
bin/
|
||||
.idea/
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
22
vendor/github.com/asaskevich/govalidator/.travis.yml
generated
vendored
22
vendor/github.com/asaskevich/govalidator/.travis.yml
generated
vendored
@@ -1,14 +1,18 @@
|
||||
dist: bionic
|
||||
language: go
|
||||
env: GO111MODULE=on GOFLAGS='-mod vendor'
|
||||
install: true
|
||||
email: false
|
||||
|
||||
go:
|
||||
- 1.1
|
||||
- 1.2
|
||||
- 1.3
|
||||
- 1.4
|
||||
- 1.5
|
||||
- 1.6
|
||||
- 1.10
|
||||
- 1.11
|
||||
- 1.12
|
||||
- 1.13
|
||||
- tip
|
||||
|
||||
notifications:
|
||||
email:
|
||||
- bwatas@gmail.com
|
||||
before_script:
|
||||
- go install github.com/golangci/golangci-lint/cmd/golangci-lint
|
||||
script:
|
||||
- golangci-lint run # run a bunch of code checkers/linters in parallel
|
||||
- go test -v -race ./... # Run all the tests with the race detector enabled
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user