|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + |
| 7 | + populatorMachinery "github.com/kubernetes-csi/lib-volume-populator/populator-machinery" |
| 8 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 9 | + |
| 10 | + "k8s.io/klog/v2" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + groupName = "provider.example.com" |
| 15 | + kind = "Provider" |
| 16 | + apiVersion = "v1alpha1" |
| 17 | + resource = "providers" |
| 18 | + prefix = "provider.example.com" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + namespace = flag.String("namespace", "provider", "Namespace to deploy controller") |
| 23 | + mode = flag.String("mode", "", "Mode to run the application in, supported values is controller") |
| 24 | + kubeconfigPath = flag.String("kubeconfig-path", "", "kubeconfig path") |
| 25 | + httpEndpoint = flag.String("http-endpoint", "", "The TCP network address where the HTTP server for diagnostics, including metrics and leader election health check, will listen (example: `:8080`). The default is empty string.") |
| 26 | + metricsPath = flag.String("metrics-path", "/metrics", "The HTTP path where prometheus metrics will be exposed. Default is `/metrics`.") |
| 27 | + |
| 28 | + groupKind = schema.GroupKind{ |
| 29 | + Group: groupName, |
| 30 | + Kind: kind, |
| 31 | + } |
| 32 | + versionResource = schema.GroupVersionResource{ |
| 33 | + Group: groupKind.Group, |
| 34 | + Version: apiVersion, |
| 35 | + Resource: resource, |
| 36 | + } |
| 37 | +) |
| 38 | + |
| 39 | +func main() { |
| 40 | + klog.InitFlags(nil) |
| 41 | + flag.Parse() |
| 42 | + |
| 43 | + switch *mode { |
| 44 | + case "controller": |
| 45 | + klog.Infof("Run provider-populator controller") |
| 46 | + pfcfg := &populatorMachinery.ProviderFunctionConfig{ |
| 47 | + PopulateFn: populateFn, |
| 48 | + PopulateCompleteFn: populateCompleteFn, |
| 49 | + PopulateCleanupFn: populateCleanupFn, |
| 50 | + } |
| 51 | + |
| 52 | + vpcfg := &populatorMachinery.VolumePopulatorConfig{ |
| 53 | + MasterURL: "", |
| 54 | + Kubeconfig: *kubeconfigPath, |
| 55 | + HttpEndpoint: *httpEndpoint, |
| 56 | + MetricsPath: *metricsPath, |
| 57 | + Namespace: *namespace, |
| 58 | + Prefix: prefix, |
| 59 | + Gk: groupKind, |
| 60 | + Gvr: versionResource, |
| 61 | + ProviderFunctionConfig: pfcfg, |
| 62 | + CrossNamespace: false, |
| 63 | + } |
| 64 | + |
| 65 | + populatorMachinery.RunControllerWithConfig(*vpcfg) |
| 66 | + default: |
| 67 | + klog.Infof("Mode %s is not supported", *mode) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func populateFn(ctx context.Context, params populatorMachinery.PopulatorParams) error { |
| 72 | + // Implement the provider-specific logic to initiate volume population. |
| 73 | + // This may involve calling cloud-native APIs or creating temporary Kubernetes resources |
| 74 | + // such as Pods or Jobs for data transfer. |
| 75 | + |
| 76 | + // Example steps: |
| 77 | + // 1. Retrieve data source details from the defined CRD. |
| 78 | + // 2. Initiate a data transfer job to params.PvcPrime using params.KubeClient. |
| 79 | + // 3. Report the volume population status to the original PVC's through params.Recorder. |
| 80 | + // 4. You should check if the transfer job already exists before creating it, otherwise the transfer job might |
| 81 | + // get created multiple times if everytime you use a unique name. |
| 82 | + |
| 83 | + klog.Infof("Run populateFn") |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +func populateCompleteFn(ctx context.Context, params populatorMachinery.PopulatorParams) (bool, error) { |
| 88 | + // Implement the provider-specific logic to determine the status of volume population. |
| 89 | + // This may involve calling cloud-native APIs or checking the completion status of |
| 90 | + // temporary Kubernetes resources like Pods or Jobs. |
| 91 | + |
| 92 | + // Example steps: |
| 93 | + // 1. Fetch the transfer job using params.KubeClient. |
| 94 | + // 2. Verify if the job has finished successfully (returns true) or is still running (returns false). |
| 95 | + // 3. If the transfer job encountered an error, evaluate the need for cleanup. |
| 96 | + // 4. Report the volume population status to the original PVC through params.Recorder. |
| 97 | + |
| 98 | + klog.Infof("Run populateCompleteFn") |
| 99 | + return true, nil |
| 100 | +} |
| 101 | + |
| 102 | +func populateCleanupFn(ctx context.Context, params populatorMachinery.PopulatorParams) error { |
| 103 | + // Implement the provider-specific logic to clean up any temporary resources |
| 104 | + // that were created during the volume population process. This step happens after PV rebind to the original PVC |
| 105 | + // and before the PVC' gets deleted. |
| 106 | + |
| 107 | + // Example steps: |
| 108 | + // 1. Fetch the transfer job using params.KubeClient. |
| 109 | + // 2. If the transfer job still exists delete the job. |
| 110 | + // 3. Report the volume population status to the original PVC through params.Recorder. |
| 111 | + |
| 112 | + klog.Infof("Run populateCleanupFn") |
| 113 | + return nil |
| 114 | +} |
0 commit comments