-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from yan234280533/dev-ensurance-2-crane-agent
add crane agent , int crane agent it contained the node-qos-ensurance-policy-controller
- Loading branch information
Showing
54 changed files
with
3,240 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/gocrane/crane/pkg/utils" | ||
"k8s.io/client-go/rest" | ||
|
||
ensuaranceapi "github.com/gocrane/api/ensurance/v1alpha1" | ||
ensuaranceset "github.com/gocrane/api/pkg/generated/clientset/versioned" | ||
"github.com/gocrane/crane/cmd/crane-agent/app/options" | ||
"github.com/gocrane/crane/pkg/ensurance/analyzer" | ||
"github.com/gocrane/crane/pkg/ensurance/avoidance" | ||
"github.com/gocrane/crane/pkg/ensurance/executor" | ||
einformer "github.com/gocrane/crane/pkg/ensurance/informer" | ||
"github.com/gocrane/crane/pkg/ensurance/manager" | ||
"github.com/gocrane/crane/pkg/ensurance/statestore" | ||
"github.com/gocrane/crane/pkg/utils/log" | ||
"github.com/spf13/cobra" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||
"k8s.io/client-go/kubernetes" | ||
clientgoscheme "k8s.io/client-go/kubernetes/scheme" | ||
) | ||
|
||
var ( | ||
scheme = runtime.NewScheme() | ||
) | ||
|
||
func init() { | ||
utilruntime.Must(clientgoscheme.AddToScheme(scheme)) | ||
utilruntime.Must(ensuaranceapi.AddToScheme(scheme)) | ||
//+kubebuilder:scaffold:scheme | ||
} | ||
|
||
// NewManagerCommand creates a *cobra.Command object with default parameters | ||
func NewManagerCommand(ctx context.Context) *cobra.Command { | ||
opts := options.NewOptions() | ||
|
||
cmd := &cobra.Command{ | ||
Use: "crane-agent", | ||
Long: `The crane agent is responsible agent in crane`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if err := opts.Complete(); err != nil { | ||
log.Logger().Error(err, "opts complete failed,exit") | ||
os.Exit(255) | ||
} | ||
if err := opts.Validate(); err != nil { | ||
log.Logger().Error(err, "opts validate failed,exit") | ||
os.Exit(255) | ||
} | ||
|
||
if err := Run(ctx, opts); err != nil { | ||
fmt.Fprintf(os.Stderr, "%v\n", err) | ||
os.Exit(1) | ||
} | ||
}, | ||
} | ||
|
||
cmd.Flags().AddGoFlagSet(flag.CommandLine) | ||
opts.AddFlags(cmd.Flags()) | ||
return cmd | ||
} | ||
|
||
// Run runs the crane-agent with options. This should never exit. | ||
func Run(ctx context.Context, opts *options.Options) error { | ||
|
||
if opts.HostnameOverride == "" { | ||
log.Logger().Error(nil, "HostnameOverride must be set as the k8s node name") | ||
os.Exit(1) | ||
} | ||
|
||
log.Logger().V(2).Info(fmt.Sprintf("opts %v", opts)) | ||
|
||
config, err := rest.InClusterConfig() | ||
if err != nil { | ||
log.Logger().Error(err, "InClusterConfig failed") | ||
} | ||
|
||
stop := utils.SetupSignalHandler() | ||
|
||
// init context | ||
ec := initializeContext(config, opts, stop) | ||
ec.Run() | ||
|
||
// init managers | ||
components := initializeManagers(opts, ec) | ||
|
||
// start managers | ||
for _, v := range components { | ||
log.Logger().V(2).Info(fmt.Sprintf("Starting manager %s", v.Name())) | ||
v.Run(ec.GetStop()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func initializeManagers(opts *options.Options, ec *einformer.Context) []manager.Manager { | ||
log.Logger().V(2).Info(fmt.Sprintf("initializeManagers")) | ||
|
||
var managers []manager.Manager | ||
podInformer := ec.GetPodInformer() | ||
nodeInformer := ec.GetNodeInformer() | ||
nepInformer := ec.GetNepInformer() | ||
avoidanceInformer := ec.GetAvoidanceInformer() | ||
|
||
var noticeCh = make(chan executor.AvoidanceExecutor) | ||
|
||
// init state store manager | ||
stateStoreManager := statestore.NewStateStoreManager(nepInformer) | ||
managers = append(managers, stateStoreManager) | ||
|
||
// init analyzer manager | ||
analyzerManager := analyzer.NewAnalyzerManager(opts.HostnameOverride, podInformer, nodeInformer, nepInformer, avoidanceInformer, stateStoreManager, ec.GetRecorder(), noticeCh) | ||
managers = append(managers, analyzerManager) | ||
|
||
// init avoidance manager | ||
avoidanceManager := avoidance.NewAvoidanceManager(ec.GetKubeClient(), opts.HostnameOverride, podInformer, nodeInformer, avoidanceInformer, noticeCh) | ||
managers = append(managers, avoidanceManager) | ||
|
||
return managers | ||
} | ||
|
||
func initializeContext(config *rest.Config, opts *options.Options, stop <-chan struct{}) *einformer.Context { | ||
log.Logger().V(2).Info(fmt.Sprintf("initializationContext")) | ||
|
||
generatedClient := kubernetes.NewForConfigOrDie(config) | ||
clientSet := ensuaranceset.NewForConfigOrDie(config) | ||
|
||
return einformer.NewContextWithClient(generatedClient, clientSet, opts.HostnameOverride, stop) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package options | ||
|
||
import ( | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
// Options hold the command-line options about crane manager | ||
type Options struct { | ||
// HostnameOverride is the name of k8s node | ||
HostnameOverride string | ||
} | ||
|
||
// NewOptions builds an empty options. | ||
func NewOptions() *Options { | ||
return &Options{} | ||
} | ||
|
||
// Complete completes all the required options. | ||
func (o *Options) Complete() error { | ||
return nil | ||
} | ||
|
||
// Validate all required options. | ||
func (o *Options) Validate() error { | ||
return nil | ||
} | ||
|
||
// AddFlags adds flags to the specified FlagSet. | ||
func (o *Options) AddFlags(flags *pflag.FlagSet) { | ||
flags.StringVar(&o.HostnameOverride, "hostname-override", "", "Which is the name of k8s node be used to filtered.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
genericapiserver "k8s.io/apiserver/pkg/server" | ||
"k8s.io/component-base/logs" | ||
|
||
"github.com/gocrane/crane/cmd/crane-agent/app" | ||
"github.com/gocrane/crane/pkg/utils/log" | ||
) | ||
|
||
// crane-agent main. | ||
func main() { | ||
logs.InitLogs() | ||
defer logs.FlushLogs() | ||
|
||
log.Init("crane-agent") | ||
|
||
ctx := genericapiserver.SetupSignalContext() | ||
|
||
if err := app.NewManagerCommand(ctx).Execute(); err != nil { | ||
fmt.Fprintf(os.Stderr, "%v\n", err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: crane-system | ||
|
||
--- | ||
apiVersion: apps/v1 | ||
kind: DaemonSet | ||
metadata: | ||
labels: | ||
app: crane-agent | ||
name: crane-agent | ||
namespace: crane-system | ||
spec: | ||
revisionHistoryLimit: 10 | ||
selector: | ||
matchLabels: | ||
app: crane-agent | ||
template: | ||
metadata: | ||
labels: | ||
app: crane-agent | ||
spec: | ||
containers: | ||
- env: | ||
- name: NODE_NAME | ||
valueFrom: | ||
fieldRef: | ||
apiVersion: v1 | ||
fieldPath: spec.nodeName | ||
image: docker.io/gocrane/crane-agent:0b506ba | ||
imagePullPolicy: Always | ||
command: | ||
- /crane-agent | ||
- -v=4 | ||
name: crane-agent | ||
restartPolicy: Always | ||
priorityClassName: system-node-critical | ||
serviceAccount: crane-agent |
Oops, something went wrong.