Merge pull request #4178 from RolandMa1986/feat-gateway-controller
feat: integrate a helm sdk that use to install gateway helm chart
This commit is contained in:
@@ -32,6 +32,7 @@ import (
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/cache"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/devops/jenkins"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/events"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/gateway"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/k8s"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/kubeedge"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/ldap"
|
||||
@@ -104,6 +105,7 @@ type Config struct {
|
||||
NotificationOptions *notification.Options `json:"notification,omitempty" yaml:"notification,omitempty" mapstructure:"notification"`
|
||||
KubeEdgeOptions *kubeedge.Options `json:"kubeedge,omitempty" yaml:"kubeedge,omitempty" mapstructure:"kubeedge"`
|
||||
MeteringOptions *metering.Options `json:"metering,omitempty" yaml:"metering,omitempty" mapstructure:"metering"`
|
||||
GatewayOptions *gateway.Options `json:"gateway,omitempty" yaml:"gateway,omitempty" mapstructure:"gateway"`
|
||||
}
|
||||
|
||||
// newConfig creates a default non-empty Config
|
||||
@@ -129,6 +131,7 @@ func New() *Config {
|
||||
AuditingOptions: auditing.NewAuditingOptions(),
|
||||
KubeEdgeOptions: kubeedge.NewKubeEdgeOptions(),
|
||||
MeteringOptions: metering.NewMeteringOptions(),
|
||||
GatewayOptions: gateway.NewGatewayOptions(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ import (
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/cache"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/devops/jenkins"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/events"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/gateway"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/k8s"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/kubeedge"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/ldap"
|
||||
@@ -179,6 +180,10 @@ func newTestConfig() (*Config, error) {
|
||||
MeteringOptions: &metering.Options{
|
||||
RetentionDay: "7d",
|
||||
},
|
||||
GatewayOptions: &gateway.Options{
|
||||
WatchesPath: "/etc/kubesphere/watches.yaml",
|
||||
Namespace: "kubesphere-controls-system",
|
||||
},
|
||||
}
|
||||
return conf, nil
|
||||
}
|
||||
|
||||
78
pkg/controller/helm/helm_controller.go
Normal file
78
pkg/controller/helm/helm_controller.go
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
Copyright 2021 The KubeSphere Authors.
|
||||
|
||||
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 helm
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"k8s.io/klog"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
|
||||
"github.com/operator-framework/helm-operator-plugins/pkg/annotation"
|
||||
"github.com/operator-framework/helm-operator-plugins/pkg/reconciler"
|
||||
"github.com/operator-framework/helm-operator-plugins/pkg/watches"
|
||||
)
|
||||
|
||||
type Reconciler struct {
|
||||
WatchFiles []string
|
||||
}
|
||||
|
||||
// SetupWithManager creates reconilers for each helm package that defined in the WatchFiles.
|
||||
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
var watchKinds []watches.Watch
|
||||
for _, file := range r.WatchFiles {
|
||||
ws, err := watches.Load(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
watchKinds = append(watchKinds, ws...)
|
||||
}
|
||||
|
||||
for _, w := range watchKinds {
|
||||
// Register controller with the factory
|
||||
reconcilePeriod := time.Minute
|
||||
if w.ReconcilePeriod != nil {
|
||||
reconcilePeriod = w.ReconcilePeriod.Duration
|
||||
}
|
||||
|
||||
maxConcurrentReconciles := runtime.NumCPU()
|
||||
if w.MaxConcurrentReconciles != nil {
|
||||
maxConcurrentReconciles = *w.MaxConcurrentReconciles
|
||||
}
|
||||
|
||||
r, err := reconciler.New(
|
||||
reconciler.WithChart(*w.Chart),
|
||||
reconciler.WithGroupVersionKind(w.GroupVersionKind),
|
||||
reconciler.WithOverrideValues(w.OverrideValues),
|
||||
reconciler.SkipDependentWatches(w.WatchDependentResources != nil && !*w.WatchDependentResources),
|
||||
reconciler.WithMaxConcurrentReconciles(maxConcurrentReconciles),
|
||||
reconciler.WithReconcilePeriod(reconcilePeriod),
|
||||
reconciler.WithInstallAnnotations(annotation.DefaultInstallAnnotations...),
|
||||
reconciler.WithUpgradeAnnotations(annotation.DefaultUpgradeAnnotations...),
|
||||
reconciler.WithUninstallAnnotations(annotation.DefaultUninstallAnnotations...),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := r.SetupWithManager(mgr); err != nil {
|
||||
return err
|
||||
}
|
||||
klog.Info("configured watch", "gvk", w.GroupVersionKind, "chartPath", w.ChartPath, "maxConcurrentReconciles", maxConcurrentReconciles, "reconcilePeriod", reconcilePeriod)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
89
pkg/controller/helm/helm_controller_test.go
Normal file
89
pkg/controller/helm/helm_controller_test.go
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
Copyright 2021 KubeSphere Authors
|
||||
|
||||
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 helm
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/klog/klogr"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
logf "sigs.k8s.io/controller-runtime/pkg/log"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"sigs.k8s.io/controller-runtime/pkg/envtest"
|
||||
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
|
||||
)
|
||||
|
||||
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
|
||||
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
|
||||
|
||||
var cfg *rest.Config
|
||||
var testEnv *envtest.Environment
|
||||
var k8sClient client.Client
|
||||
|
||||
func TestApplicationController(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecsWithDefaultAndCustomReporters(t,
|
||||
"Application Controller Test Suite",
|
||||
[]Reporter{printer.NewlineReporter{}})
|
||||
}
|
||||
|
||||
var _ = BeforeSuite(func(done Done) {
|
||||
logf.SetLogger(klogr.New())
|
||||
|
||||
By("bootstrapping test environment")
|
||||
testEnv = &envtest.Environment{
|
||||
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crds")},
|
||||
AttachControlPlaneOutput: false,
|
||||
}
|
||||
var err error
|
||||
cfg, err = testEnv.Start()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(cfg).ToNot(BeNil())
|
||||
|
||||
close(done)
|
||||
}, 60)
|
||||
|
||||
var _ = AfterSuite(func() {
|
||||
By("tearing down the test environment")
|
||||
err := testEnv.Stop()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
var _ = Context("Helm reconcier", func() {
|
||||
Describe("Gateway", func() {
|
||||
It("Should setup gateway helm reconcier", func() {
|
||||
data := "- group: gateway.kubesphere.io\n version: v1alpha1\n kind: Gateway\n chart: ../../../config/gateway\n"
|
||||
f, _ := ioutil.TempFile("", "watch")
|
||||
ioutil.WriteFile(f.Name(), []byte(data), 0)
|
||||
|
||||
mgr, err := ctrl.NewManager(cfg, ctrl.Options{MetricsBindAddress: "0"})
|
||||
Expect(err).NotTo(HaveOccurred(), "failed to create a manager")
|
||||
|
||||
reconciler := &Reconciler{}
|
||||
reconciler.WatchFiles = append(reconciler.WatchFiles, f.Name())
|
||||
err = reconciler.SetupWithManager(mgr)
|
||||
Expect(err).NotTo(HaveOccurred(), "failed to setup helm reconciler")
|
||||
|
||||
})
|
||||
})
|
||||
})
|
||||
62
pkg/simple/client/gateway/options.go
Normal file
62
pkg/simple/client/gateway/options.go
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
Copyright 2021 KubeSphere Authors
|
||||
|
||||
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 gateway
|
||||
|
||||
import (
|
||||
"github.com/spf13/pflag"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/utils/reflectutils"
|
||||
)
|
||||
|
||||
// Options contains configuration of the default Gateway
|
||||
type Options struct {
|
||||
WatchesPath string `json:"watchesPath,omitempty" yaml:"watchesPath"`
|
||||
Namespace string `json:"namespace,omitempty" yaml:"namespace"`
|
||||
}
|
||||
|
||||
// NewGatewayOptions creates a default Gateway Option
|
||||
func NewGatewayOptions() *Options {
|
||||
return &Options{
|
||||
WatchesPath: "",
|
||||
Namespace: "", //constants.KubeSphereControlNamespace
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Options) IsEmpty() bool {
|
||||
return s.WatchesPath == ""
|
||||
}
|
||||
|
||||
// Validate check options values
|
||||
func (s *Options) Validate() []error {
|
||||
var errors []error
|
||||
|
||||
return errors
|
||||
}
|
||||
|
||||
// ApplyTo overrides options if it's valid, which watchesPath is not empty
|
||||
func (s *Options) ApplyTo(options *Options) {
|
||||
if s.WatchesPath != "" {
|
||||
reflectutils.Override(options, s)
|
||||
}
|
||||
}
|
||||
|
||||
// AddFlags add options flags to command line flags,
|
||||
// if watchesPath left empty, following options will be ignored
|
||||
func (s *Options) AddFlags(fs *pflag.FlagSet, c *Options) {
|
||||
fs.StringVar(&s.WatchesPath, "watches-path", c.WatchesPath, "Path to the watches file to use.")
|
||||
fs.StringVar(&s.Namespace, "namespace", c.Namespace, "Working Namespace of the Gateway's Ingress Controller.")
|
||||
}
|
||||
Reference in New Issue
Block a user