Skip to content

Commit

Permalink
Merge pull request openyurtio#16 from rambohe-ch/master
Browse files Browse the repository at this point in the history
fix golint warnning
  • Loading branch information
huangyuqi authored May 28, 2020
2 parents 679cfbc + 3eb49d5 commit 781cba8
Show file tree
Hide file tree
Showing 34 changed files with 517 additions and 473 deletions.
74 changes: 12 additions & 62 deletions cmd/yurt-controller-manager/app/controllermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ import (
"github.com/alibaba/openyurt/cmd/yurt-controller-manager/app/options"
yurtctrlmgrconfig "github.com/alibaba/openyurt/pkg/controller/apis/config"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime/schema"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/apimachinery/pkg/util/wait"
Expand All @@ -45,9 +43,7 @@ import (
"k8s.io/apiserver/pkg/server/mux"
serveroptions "k8s.io/apiserver/pkg/server/options"
"k8s.io/apiserver/pkg/util/term"
cacheddiscovery "k8s.io/client-go/discovery/cached"
"k8s.io/client-go/informers"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
cliflag "k8s.io/component-base/cli/flag"
Expand All @@ -61,7 +57,7 @@ import (
)

const (
// Jitter used when starting controller managers
// ControllerStartJitter used when starting controller managers
ControllerStartJitter = 1.0
)

Expand Down Expand Up @@ -232,6 +228,7 @@ func Run(c *config.CompletedConfig, stopCh <-chan struct{}) error {
panic("unreachable")
}

// ControllerContext is an context for all controllers
type ControllerContext struct {
// ClientBuilder will provide a client for this controller to use
ClientBuilder controller.ControllerClientBuilder
Expand All @@ -242,14 +239,6 @@ type ControllerContext struct {
// ComponentConfig provides access to init options for a given controller
ComponentConfig yurtctrlmgrconfig.YurtControllerManagerConfiguration

// DeferredDiscoveryRESTMapper is a RESTMapper that will defer
// initialization of the RESTMapper until the first mapping is
// requested.
RESTMapper *restmapper.DeferredDiscoveryRESTMapper

// AvailableResources is a map listing currently available resources
AvailableResources map[schema.GroupVersionResource]bool

// Stop is the stop channel
Stop <-chan struct{}

Expand All @@ -263,6 +252,7 @@ type ControllerContext struct {
ResyncPeriod func() time.Duration
}

// IsControllerEnabled used for verifing specified controller is enabled or not
func (c ControllerContext) IsControllerEnabled(name string) bool {
return genericcontrollermanager.IsControllerEnabled(name, ControllersDisabledByDefault, c.ComponentConfig.Generic.Controllers)
}
Expand All @@ -272,12 +262,14 @@ func (c ControllerContext) IsControllerEnabled(name string) bool {
// The bool indicates whether the controller was enabled.
type InitFunc func(ctx ControllerContext) (debuggingHandler http.Handler, enabled bool, err error)

// KnownControllers returns all controllers name
func KnownControllers() []string {
ret := sets.StringKeySet(NewControllerInitializers())

return ret.List()
}

// ControllersDisabledByDefault represents all of default disabled controller
var ControllersDisabledByDefault = sets.NewString()

//const (
Expand All @@ -293,34 +285,6 @@ func NewControllerInitializers() map[string]InitFunc {
return controllers
}

// TODO: In general, any controller checking this needs to be dynamic so
// users don't have to restart their controller manager if they change the apiserver.
// Until we get there, the structure here needs to be exposed for the construction of a proper ControllerContext.
func GetAvailableResources(clientBuilder controller.ControllerClientBuilder) (map[schema.GroupVersionResource]bool, error) {
client := clientBuilder.ClientOrDie("controller-discovery")
discoveryClient := client.Discovery()
resourceMap, err := discoveryClient.ServerResources()
if err != nil {
utilruntime.HandleError(fmt.Errorf("unable to get all supported resources from server: %v", err))
}
if len(resourceMap) == 0 {
return nil, fmt.Errorf("unable to get any supported resources from server")
}

allResources := map[schema.GroupVersionResource]bool{}
for _, apiResourceList := range resourceMap {
version, err := schema.ParseGroupVersion(apiResourceList.GroupVersion)
if err != nil {
return nil, err
}
for _, apiResource := range apiResourceList.APIResources {
allResources[version.WithResource(apiResource.Name)] = true
}
}

return allResources, nil
}

// CreateControllerContext creates a context struct containing references to resources needed by the
// controllers such as the cloud provider and clientBuilder. rootClientBuilder is only used for
// the shared-informers client and token controller.
Expand All @@ -334,32 +298,18 @@ func CreateControllerContext(s *config.CompletedConfig, rootClientBuilder, clien
return ControllerContext{}, fmt.Errorf("failed to wait for apiserver being healthy: %v", err)
}

// Use a discovery client capable of being refreshed.
discoveryClient := rootClientBuilder.ClientOrDie("controller-discovery")
cachedClient := cacheddiscovery.NewMemCacheClient(discoveryClient.Discovery())
restMapper := restmapper.NewDeferredDiscoveryRESTMapper(cachedClient)
go wait.Until(func() {
restMapper.Reset()
}, 30*time.Second, stop)

availableResources, err := GetAvailableResources(rootClientBuilder)
if err != nil {
return ControllerContext{}, err
}

ctx := ControllerContext{
ClientBuilder: clientBuilder,
InformerFactory: sharedInformers,
ComponentConfig: s.ComponentConfig,
RESTMapper: restMapper,
AvailableResources: availableResources,
Stop: stop,
InformersStarted: make(chan struct{}),
ResyncPeriod: ResyncPeriod(s),
ClientBuilder: clientBuilder,
InformerFactory: sharedInformers,
ComponentConfig: s.ComponentConfig,
Stop: stop,
InformersStarted: make(chan struct{}),
ResyncPeriod: ResyncPeriod(s),
}
return ctx, nil
}

// StartControllers starts all of enabled controllers
func StartControllers(ctx ControllerContext, controllers map[string]InitFunc, unsecuredMux *mux.PathRecorderMux) error {
for controllerName, initFn := range controllers {
if !ctx.IsControllerEnabled(controllerName) {
Expand Down
2 changes: 2 additions & 0 deletions cmd/yurthub/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"k8s.io/klog"
)

// YurtHubConfiguration represents configuration of yurthub
type YurtHubConfiguration struct {
LBMode string
RemoteServers []*url.URL
Expand All @@ -24,6 +25,7 @@ type YurtHubConfiguration struct {
MaxRequestInFlight int
}

// Complete converts *options.YurtHubOptions to *YurtHubConfiguration
func Complete(options *options.YurtHubOptions) (*YurtHubConfiguration, error) {
us, err := parseRemoteServers(options.ServerAddr)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions cmd/yurthub/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/spf13/pflag"
)

// YurtHubOptions is the main settings for the yurthub
type YurtHubOptions struct {
ServerAddr string
YurtHubHost string
Expand All @@ -21,6 +22,7 @@ type YurtHubOptions struct {
MaxRequestInFlight int
}

// NewYurtHubOptions creates a new YurtHubOptions with a default config.
func NewYurtHubOptions() *YurtHubOptions {
o := &YurtHubOptions{
YurtHubHost: "127.0.0.1",
Expand All @@ -37,6 +39,7 @@ func NewYurtHubOptions() *YurtHubOptions {
return o
}

// ValidateOptions validates YurtHubOptions
func ValidateOptions(options *YurtHubOptions) error {
if len(options.NodeName) == 0 {
return fmt.Errorf("node name is empty")
Expand All @@ -57,6 +60,7 @@ func ValidateOptions(options *YurtHubOptions) error {
return nil
}

// AddFlags returns flags for a specific yurthub by section name
func (o *YurtHubOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.YurtHubHost, "yurt-hub-host", o.YurtHubHost, "the host that used to connect yurthub.")
fs.IntVar(&o.YurtHubPort, "yurt-hub-port", o.YurtHubPort, "the port that used to connect yurthub.")
Expand Down
4 changes: 2 additions & 2 deletions cmd/yurthub/app/start.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package app

import (
_ "net/http/pprof"

"github.com/alibaba/openyurt/cmd/yurthub/app/config"
"github.com/alibaba/openyurt/cmd/yurthub/app/options"
"github.com/alibaba/openyurt/pkg/yurthub/cachemanager"
Expand All @@ -27,6 +25,7 @@ const (
componentYurtHub = "yurthub"
)

// NewCmdStartYurtHub creates a *cobra.Command object with default parameters
func NewCmdStartYurtHub(stopCh <-chan struct{}) *cobra.Command {
yurtHubOptions := options.NewYurtHubOptions()

Expand Down Expand Up @@ -58,6 +57,7 @@ func NewCmdStartYurtHub(stopCh <-chan struct{}) *cobra.Command {
return cmd
}

// Run runs the YurtHubConfiguration. This should never exit
func Run(cfg *config.YurtHubConfiguration, stopCh <-chan struct{}) error {
trace := 1
klog.Infof("%d. new transport manager for healthz client", trace)
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/nodelifecycle/scheduler/taint_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const (
podUpdateChannelSize = 1
retries = 5

// NodeAutonomyIdentify is an annotation key for node.
// AnnotationKeyNodeAutonomy is an annotation key for node autonomy.
AnnotationKeyNodeAutonomy = "node.beta.alibabacloud.com/autonomy"
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/yurtctl/util/kubernetes/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

batchv1 "k8s.io/api/batch/v1"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
Expand Down Expand Up @@ -75,7 +75,7 @@ func RunJobAndCleanup(cliSet *kubernetes.Clientset, job *batchv1.Job, timeout, p
job, err := cliSet.BatchV1().Jobs(job.GetNamespace()).
Get(job.GetName(), metav1.GetOptions{})
if err != nil {
klog.Error("fail to get job(%s) when waiting for it to be succeeded: %s",
klog.Errorf("fail to get job(%s) when waiting for it to be succeeded: %s",
job.GetName(), err)
return err
}
Expand Down
Loading

0 comments on commit 781cba8

Please sign in to comment.