generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial implementation to add external health monitor controller and agent #3
Merged
k8s-ci-robot
merged 4 commits into
kubernetes-csi:master
from
NickrenREN:initial-implementation
Jul 6, 2020
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#! /bin/bash | ||
|
||
. release-tools/prow.sh | ||
|
||
main |
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,6 @@ | ||
FROM gcr.io/distroless/static:latest | ||
LABEL maintainers="Kubernetes Authors" | ||
LABEL description="CSI External Health Monitor Agent" | ||
|
||
COPY ./bin/csi-external-health-monitor-agent csi-external-health-monitor-agent | ||
ENTRYPOINT ["/csi-external-health-monitor-agent"] |
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,209 @@ | ||
/* | ||
Copyright 2020 The Kubernetes 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 main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"k8s.io/client-go/informers" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
"k8s.io/klog" | ||
|
||
"github.com/container-storage-interface/spec/lib/go/csi" | ||
"github.com/kubernetes-csi/csi-lib-utils/connection" | ||
"github.com/kubernetes-csi/csi-lib-utils/metrics" | ||
"github.com/kubernetes-csi/csi-lib-utils/rpc" | ||
"google.golang.org/grpc" | ||
|
||
monitoragent "github.com/kubernetes-csi/external-health-monitor/pkg/agent" | ||
) | ||
|
||
const ( | ||
|
||
// Default timeout of short CSI calls like GetPluginInfo | ||
csiTimeout = time.Second | ||
) | ||
|
||
// Command line flags | ||
var ( | ||
monitorInterval = flag.Duration("monitor-interval", 1*time.Minute, "Interval for controller to check volumes health condition.") | ||
|
||
kubeconfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig file. Required only when running out of cluster.") | ||
resync = flag.Duration("resync", 10*time.Minute, "Resync interval of the controller.") | ||
csiAddress = flag.String("csi-address", "/run/csi/socket", "Address of the CSI driver socket.") | ||
showVersion = flag.Bool("version", false, "Show version.") | ||
timeout = flag.Duration("timeout", 15*time.Second, "Timeout for waiting for attaching or detaching the volume.") | ||
workerThreads = flag.Uint("worker-threads", 10, "Number of pv monitor worker threads") | ||
kubeletRootPath = flag.String("kubelet-root-path", "/var/lib/kubelet", "The root path of kubelet.") | ||
|
||
metricsAddress = flag.String("metrics-address", "", "The TCP network address where the prometheus metrics endpoint will listen (example: `:8080`). The default is empty string, which means metrics endpoint is disabled.") | ||
metricsPath = flag.String("metrics-path", "/metrics", "The HTTP path where prometheus metrics will be exposed. Default is `/metrics`.") | ||
) | ||
|
||
var ( | ||
version = "unknown" | ||
) | ||
|
||
func main() { | ||
klog.InitFlags(nil) | ||
flag.Set("logtostderr", "true") | ||
flag.Parse() | ||
|
||
if *showVersion { | ||
fmt.Println(os.Args[0], version) | ||
return | ||
} | ||
klog.Infof("Version: %s", version) | ||
|
||
// Create the client config. Use kubeconfig if given, otherwise assume in-cluster. | ||
config, err := buildConfig(*kubeconfig) | ||
if err != nil { | ||
klog.Error(err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
if *workerThreads == 0 { | ||
klog.Error("option -worker-threads must be greater than zero") | ||
os.Exit(1) | ||
} | ||
|
||
clientset, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
klog.Error(err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
factory := informers.NewSharedInformerFactory(clientset, *resync) | ||
|
||
metricsManager := metrics.NewCSIMetricsManager("" /* driverName */) | ||
|
||
// Connect to CSI. | ||
csiConn, err := connection.Connect(*csiAddress, metricsManager, connection.OnConnectionLoss(connection.ExitOnConnectionLoss())) | ||
if err != nil { | ||
klog.Error(err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
err = rpc.ProbeForever(csiConn, *timeout) | ||
if err != nil { | ||
klog.Error(err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
// Find driver name. | ||
ctx, cancel := context.WithTimeout(context.Background(), csiTimeout) | ||
defer cancel() | ||
storageDriver, err := rpc.GetDriverName(ctx, csiConn) | ||
if err != nil { | ||
klog.Error(err.Error()) | ||
os.Exit(1) | ||
} | ||
klog.V(2).Infof("CSI driver name: %q", storageDriver) | ||
metricsManager.SetDriverName(storageDriver) | ||
metricsManager.StartMetricsEndpoint(*metricsAddress, *metricsPath) | ||
|
||
supportNodeGetVolumeCondition, err := supportNodeGetVolumeCondition(ctx, csiConn) | ||
if err != nil { | ||
klog.Error(err.Error()) | ||
os.Exit(1) | ||
} | ||
if !supportNodeGetVolumeCondition { | ||
klog.V(2).Infof("CSI driver does not support Node VolumeCondition Capability, exiting") | ||
os.Exit(1) | ||
} | ||
|
||
supportStageUnstage, err := supportStageUnstage(ctx, csiConn) | ||
if err != nil { | ||
klog.Error(err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
monitorAgent := monitoragent.NewPVMonitorAgent(clientset, storageDriver, csiConn, *timeout, *monitorInterval, factory.Core().V1().PersistentVolumes(), | ||
factory.Core().V1().PersistentVolumeClaims(), factory.Core().V1().Pods(), supportStageUnstage, *kubeletRootPath) | ||
|
||
run := func(ctx context.Context) { | ||
stopCh := ctx.Done() | ||
factory.Start(stopCh) | ||
monitorAgent.Run(int(*workerThreads), stopCh) | ||
} | ||
|
||
run(context.TODO()) | ||
} | ||
|
||
func buildConfig(kubeconfig string) (*rest.Config, error) { | ||
if kubeconfig != "" { | ||
return clientcmd.BuildConfigFromFlags("", kubeconfig) | ||
} | ||
return rest.InClusterConfig() | ||
} | ||
|
||
func supportNodeGetVolumeCondition(ctx context.Context, csiConn *grpc.ClientConn) (supportNodeGetVolumeCondition bool, err error) { | ||
client := csi.NewNodeClient(csiConn) | ||
req := csi.NodeGetCapabilitiesRequest{} | ||
rsp, err := client.NodeGetCapabilities(ctx, &req) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
for _, cap := range rsp.GetCapabilities() { | ||
if cap == nil { | ||
continue | ||
} | ||
rpc := cap.GetRpc() | ||
if rpc == nil { | ||
continue | ||
} | ||
t := rpc.GetType() | ||
if t == csi.NodeServiceCapability_RPC_VOLUME_CONDITION { | ||
return true, nil | ||
} | ||
} | ||
|
||
return false, nil | ||
} | ||
|
||
func supportStageUnstage(ctx context.Context, csiConn *grpc.ClientConn) (supportStageUnstage bool, err error) { | ||
client := csi.NewNodeClient(csiConn) | ||
req := csi.NodeGetCapabilitiesRequest{} | ||
rsp, err := client.NodeGetCapabilities(ctx, &req) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
for _, cap := range rsp.GetCapabilities() { | ||
if cap == nil { | ||
continue | ||
} | ||
|
||
rpc := cap.GetRpc() | ||
if rpc == nil { | ||
continue | ||
} | ||
t := rpc.GetType() | ||
if t == csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME { | ||
return true, nil | ||
} | ||
} | ||
|
||
return false, nil | ||
} |
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,6 @@ | ||
FROM gcr.io/distroless/static:latest | ||
LABEL maintainers="Kubernetes Authors" | ||
LABEL description="CSI External Health Monitor Controller" | ||
|
||
COPY ./bin/csi-external-health-monitor-controller csi-external-health-monitor-controller | ||
ENTRYPOINT ["/csi-external-health-monitor-controller"] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add retryIntervalStart and retryIntervalMax?
https://github.com/kubernetes-csi/external-attacher/blob/master/cmd/csi-attacher/main.go#L61