This is a huge commit, it does following things:
1. refactor kubesphere dependency service client creation, we can disable dependency by config 2. dependencies can be configured by configuration file 3. refactor cmd package using cobra.Command, so we can use hypersphere to invoke command sepearately. Later we only need to build one image to contains all kubesphere core components. One command to rule them all! 4. live reloading configuration currently not implemented
This commit is contained in:
109
pkg/simple/client/devops/devops.go
Normal file
109
pkg/simple/client/devops/devops.go
Normal file
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
Copyright 2018 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 devops
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"k8s.io/klog"
|
||||
"kubesphere.io/kubesphere/pkg/gojenkins"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const (
|
||||
JenkinsAllUserRoleName = "kubesphere-user"
|
||||
)
|
||||
|
||||
type DevopsClient struct {
|
||||
jenkinsClient *gojenkins.Jenkins
|
||||
}
|
||||
|
||||
func NewDevopsClient(options *DevopsOptions) (*DevopsClient, error) {
|
||||
var d DevopsClient
|
||||
|
||||
jenkins := gojenkins.CreateJenkins(nil, options.Host, options.MaxConnections, options.Username, options.Password)
|
||||
jenkins, err := jenkins.Init()
|
||||
if err != nil {
|
||||
klog.Errorf("failed to connecto to jenkins role, %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
d.jenkinsClient = jenkins
|
||||
|
||||
err = d.initializeJenkins()
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &d, nil
|
||||
}
|
||||
|
||||
func NewDevopsClientOrDie(options *DevopsOptions) *DevopsClient {
|
||||
jenkins := gojenkins.CreateJenkins(nil, options.Host, options.MaxConnections, options.Username, options.Password)
|
||||
jenkins, err := jenkins.Init()
|
||||
if err != nil {
|
||||
klog.Errorf("failed to connecto to jenkins role, %+v", err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
d := &DevopsClient{
|
||||
jenkinsClient: jenkins,
|
||||
}
|
||||
|
||||
err = d.initializeJenkins()
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
func (c *DevopsClient) Jenkins() *gojenkins.Jenkins {
|
||||
return c.jenkinsClient
|
||||
}
|
||||
|
||||
var mutex = sync.Mutex{}
|
||||
|
||||
func (c *DevopsClient) initializeJenkins() error {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
if c.jenkinsClient == nil {
|
||||
return fmt.Errorf("jenkins intialization failed")
|
||||
}
|
||||
|
||||
globalRole, err := c.jenkinsClient.GetGlobalRole(JenkinsAllUserRoleName)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Jenkins uninitialized, create global role
|
||||
if globalRole == nil {
|
||||
_, err := c.jenkinsClient.AddGlobalRole(JenkinsAllUserRoleName, gojenkins.GlobalPermissionIds{GlobalRead: true}, true)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
_, err = c.jenkinsClient.AddProjectRole(JenkinsAllUserRoleName, "\\n\\s*\\r", gojenkins.ProjectPermissionIds{SCMTag: true}, true)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
81
pkg/simple/client/devops/options.go
Normal file
81
pkg/simple/client/devops/options.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package devops
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
type DevopsOptions struct {
|
||||
Host string `json:",omitempty" yaml:",omitempty" description:"Jenkins service host address"`
|
||||
Username string `json:",omitempty" yaml:",omitempty" description:"Jenkins admin username"`
|
||||
Password string `json:",omitempty" yaml:",omitempty" description:"Jenkins admin password"`
|
||||
MaxConnections int `json:"maxConnections,omitempty" yaml:"maxConnections,omitempty" description:"Maximum connections allowed to connect to Jenkins"`
|
||||
}
|
||||
|
||||
// NewDevopsOptions returns a `zero` instance
|
||||
func NewDevopsOptions() *DevopsOptions {
|
||||
return &DevopsOptions{
|
||||
Host: "",
|
||||
Username: "",
|
||||
Password: "",
|
||||
MaxConnections: 100,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *DevopsOptions) ApplyTo(options *DevopsOptions) {
|
||||
if options == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if s.Host != "" {
|
||||
options.Host = s.Host
|
||||
}
|
||||
|
||||
if s.Username != "" {
|
||||
options.Username = s.Username
|
||||
}
|
||||
|
||||
if s.Password != "" {
|
||||
options.Password = s.Password
|
||||
}
|
||||
|
||||
if s.MaxConnections > 0 {
|
||||
options.MaxConnections = s.MaxConnections
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
func (s *DevopsOptions) Validate() []error {
|
||||
errors := []error{}
|
||||
|
||||
// devops is not needed, ignore rest options
|
||||
if s.Host == "" {
|
||||
return errors
|
||||
}
|
||||
|
||||
if s.Username == "" || s.Password == "" {
|
||||
errors = append(errors, fmt.Errorf("jenkins's username or password is empty"))
|
||||
}
|
||||
|
||||
if s.MaxConnections <= 0 {
|
||||
errors = append(errors, fmt.Errorf("jenkins's maximum connections should be greater than 0"))
|
||||
}
|
||||
|
||||
return errors
|
||||
}
|
||||
|
||||
func (s *DevopsOptions) AddFlags(fs *pflag.FlagSet) {
|
||||
fs.StringVar(&s.Host, "jenkins-host", s.Host, ""+
|
||||
"Jenkins service host address. If left blank, means Jenkins "+
|
||||
"is unnecessary.")
|
||||
|
||||
fs.StringVar(&s.Username, "jenkins-username", s.Username, ""+
|
||||
"Username for access to Jenkins service. Leave it blank if there isn't any.")
|
||||
|
||||
fs.StringVar(&s.Password, "jenkins-password", s.Password, ""+
|
||||
"Password for access to Jenkins service, used pair with username.")
|
||||
|
||||
fs.IntVar(&s.MaxConnections, "jenkins-max-connections", s.MaxConnections, ""+
|
||||
"Maximum allowed connections to Jenkins. ")
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user