-
Notifications
You must be signed in to change notification settings - Fork 1
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
ADD Dataload-Manager Model #16
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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
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,5 @@ | ||
FROM alpine:latest | ||
|
||
COPY ./_build/dataload-init / | ||
|
||
ENTRYPOINT [ "/dataload-init" ] |
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 @@ | ||
FROM alpine:latest | ||
|
||
COPY ./_build/datamanager / | ||
|
||
ENTRYPOINT [ "/dataload-init" ] |
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,9 @@ | ||
FROM centos:7 | ||
|
||
RUN yum install -y wget | ||
RUN wget https://github.com/juicedata/juicesync/releases/download/v1.1.1/juicesync-1.1.1-linux-arm64.tar.gz | ||
RUN tar zxf juicesync-1.1.1-linux-arm64.tar.gz && mv juicesync /usr/bin && chmod +x /usr/bin/juicesync | ||
|
||
COPY ./_build/dataload-manager / | ||
|
||
ENTRYPOINT [ "/dataload-manager" ] |
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,8 @@ | ||
FROM alpine:latest | ||
|
||
RUN wget https://github.com/juicedata/juicesync/releases/download/v1.1.1/juicesync-1.1.1-linux-arm64.tar.gz | ||
RUN tar zxf juicesync-1.1.1-linux-arm64.tar.gz && mv juicesync /usr/bin && chmod +x /usr/bin/juicesync | ||
|
||
COPY ./_build/dataload-manager / | ||
|
||
ENTRYPOINT [ "/dataload-manager" ] |
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,140 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
datastorev1alpha1 "github.com/hwameistor/datastore/pkg/apis/client/clientset/versioned/typed/datastore/v1alpha1" | ||
datastore "github.com/hwameistor/datastore/pkg/apis/datastore/v1alpha1" | ||
log "github.com/sirupsen/logrus" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/watch" | ||
"k8s.io/client-go/kubernetes" | ||
v1 "k8s.io/client-go/kubernetes/typed/core/v1" | ||
"k8s.io/client-go/rest" | ||
"os" | ||
"time" | ||
) | ||
|
||
var ( | ||
nodeName = flag.String("nodename", "", "Node name") | ||
subDir = flag.String("subdir", "", "subdir") | ||
) | ||
|
||
const ( | ||
NameSpaceEnvVar = "NAMESPACE" | ||
pvcNameEnvVar = "PVC_NAME" | ||
apiGroup = "example.com" | ||
version = "v1alpha1" | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
if *nodeName == "" { | ||
log.WithFields(log.Fields{"nodename": *nodeName}).Error("Invalid node name") | ||
os.Exit(1) | ||
} | ||
if *subDir == "" { | ||
log.WithFields(log.Fields{"subdir": *subDir}).Error("Invalid subdir path") | ||
os.Exit(1) | ||
} | ||
namespace := os.Getenv(NameSpaceEnvVar) | ||
pvcName := os.Getenv(pvcNameEnvVar) | ||
|
||
if namespace == "" || pvcName == "" { | ||
log.Fatal("Namespace or PVC Name environment variables are not set.") | ||
} | ||
|
||
config, err := getConfig() | ||
if err != nil { | ||
log.WithError(err).Fatal("Failed to get Kubernetes configuration") | ||
} | ||
|
||
clientset, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
log.WithError(err).Fatal("Failed to create Kubernetes clientset") | ||
} | ||
|
||
pvcClient := clientset.CoreV1().PersistentVolumeClaims(namespace) | ||
pvc, err := getPersistentVolumeClaim(pvcClient, pvcName) | ||
if err != nil { | ||
log.WithError(err).Error("Failed to get PVC") | ||
return | ||
} | ||
|
||
dataSetName := pvc.Spec.VolumeName | ||
dataLoadRequest := createDataLoadRequest(dataSetName, *subDir) | ||
dsClient, err := datastorev1alpha1.NewForConfig(config) | ||
watcher, err := watchCustomResource(dsClient, namespace, dataSetName) | ||
if err != nil { | ||
log.WithError(err).Error("Failed to start watching custom resource") | ||
return | ||
} | ||
defer watcher.Stop() | ||
// 开始计时 | ||
peng9808 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
start := time.Now() | ||
if err := createCustomResource(dsClient, dataLoadRequest, namespace); err != nil { | ||
log.WithError(err).Error("Failed to create custom resource") | ||
return | ||
} | ||
fmt.Println("Created custom resource") | ||
peng9808 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for event := range watcher.ResultChan() { | ||
if event.Type == watch.Deleted { | ||
fmt.Println("Custom resource deleted, exiting") | ||
//计时结束 | ||
end := time.Now() | ||
duration := end.Sub(start) | ||
fmt.Printf("DataLoad execution time: %s\n", duration) | ||
return | ||
} | ||
} | ||
} | ||
|
||
func getConfig() (*rest.Config, error) { | ||
config, err := rest.InClusterConfig() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get in-cluster config: %w", err) | ||
} | ||
return config, nil | ||
} | ||
|
||
func getPersistentVolumeClaim(pvcClient v1.PersistentVolumeClaimInterface, pvcName string) (*corev1.PersistentVolumeClaim, error) { | ||
return pvcClient.Get(context.TODO(), pvcName, metav1.GetOptions{}) | ||
} | ||
|
||
func createDataLoadRequest(dataSetName, subDir string) *datastore.DataLoadRequest { | ||
return &datastore.DataLoadRequest{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: fmt.Sprintf("%s/%s", apiGroup, version), | ||
Kind: "DataLoadRequest", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: dataSetName, | ||
}, | ||
Spec: datastore.DataLoadRequestSpec{ | ||
IsGlobal: true, | ||
Node: *nodeName, | ||
DataSet: dataSetName, | ||
SubDir: subDir, | ||
}, | ||
Status: datastore.DataLoadRequestStatus{ | ||
State: datastore.OperationStateStart, | ||
}, | ||
} | ||
} | ||
|
||
func createCustomResource(dsClient datastorev1alpha1.DatastoreV1alpha1Interface, dataLoadRequest *datastore.DataLoadRequest, namespace string) error { | ||
_, err := dsClient.DataLoadRequests(namespace).Create(context.Background(), dataLoadRequest, metav1.CreateOptions{}) | ||
if err != nil && !errors.IsAlreadyExists(err) { | ||
return fmt.Errorf("failed to create custom resource: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func watchCustomResource(dsClient datastorev1alpha1.DatastoreV1alpha1Interface, namespace, resourceName string) (watch.Interface, error) { | ||
return dsClient.DataLoadRequests(namespace).Watch(context.TODO(), metav1.ListOptions{ | ||
FieldSelector: fmt.Sprintf("metadata.name=%s", resourceName), | ||
}) | ||
} |
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,105 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
dataloadManager "github.com/hwameistor/datastore/pkg/dataload-manager" | ||
"github.com/kubernetes-csi/csi-lib-utils/leaderelection" | ||
log "github.com/sirupsen/logrus" | ||
"k8s.io/client-go/informers" | ||
"strings" | ||
|
||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
"k8s.io/klog/v2" | ||
"os" | ||
"time" | ||
|
||
dsclientset "github.com/hwameistor/datastore/pkg/apis/client/clientset/versioned" | ||
dsinformers "github.com/hwameistor/datastore/pkg/apis/client/informers/externalversions" | ||
) | ||
|
||
var ( | ||
showVersion = flag.Bool("version", false, "Show version.") | ||
enableLeaderElection = flag.Bool("leader-election", false, "Enable leader election.") | ||
kubeconfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig file. Required only when running out of cluster.") | ||
rsync = flag.Duration("rsync", 10*time.Minute, "Rsync interval of the controller.") | ||
nodeName = flag.String("nodename", "", "Node name") | ||
version = "unknown" | ||
) | ||
|
||
func main() { | ||
klog.InitFlags(nil) | ||
flag.Set("logtostderr", "true") | ||
flag.Parse() | ||
klog.Infof("args: %s", strings.Join(os.Args, " ")) | ||
|
||
if *showVersion { | ||
fmt.Println(os.Args[0], version) | ||
return | ||
} | ||
klog.Infof("Version: %s", version) | ||
|
||
if *nodeName == "" { | ||
log.WithFields(log.Fields{"nodename": *nodeName}).Error("Invalid node name") | ||
os.Exit(1) | ||
} | ||
|
||
// 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) | ||
} | ||
|
||
// Create the kubeClientset for in-cluster resources | ||
kubeClientset, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
klog.Error(err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
// Create the kubeClientset for datastore resources | ||
dsClient, err := dsclientset.NewForConfig(config) | ||
if err != nil { | ||
klog.Error(err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
// Create the kubeClientset for datastore resources | ||
coreFactory := informers.NewSharedInformerFactory(kubeClientset, *rsync) | ||
dsFactory := dsinformers.NewSharedInformerFactory(dsClient, *rsync) | ||
dataLoadRequestInformer := dsFactory.Datastore().V1alpha1().DataLoadRequests() | ||
|
||
ctr := dataloadManager.New(*nodeName, kubeClientset, dsClient, dataLoadRequestInformer) | ||
run := func(ctx context.Context) { | ||
stopCh := ctx.Done() | ||
dsFactory.Start(stopCh) | ||
coreFactory.Start(stopCh) | ||
ctr.Run(stopCh) | ||
} | ||
|
||
leClientset, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
klog.Fatalf("Failed to create leaderelection client: %v", err) | ||
} | ||
|
||
if *enableLeaderElection { | ||
lockName := "hwameistor-dataload-manager-node" | ||
le := leaderelection.NewLeaderElection(leClientset, lockName, run) | ||
if err = le.Run(); err != nil { | ||
klog.Fatalf("Failed to initialize leader election: %v", err) | ||
} | ||
} else { | ||
run(context.TODO()) | ||
} | ||
} | ||
|
||
func buildConfig(kubeconfig string) (*rest.Config, error) { | ||
if kubeconfig != "" { | ||
return clientcmd.BuildConfigFromFlags("", kubeconfig) | ||
} | ||
return rest.InClusterConfig() | ||
} |
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.
alpine is a better choice for base-image than centos :-)