yangwwei commented on a change in pull request #72: Add ability to support 3rd party K8s operator integration URL: https://github.com/apache/incubator-yunikorn-k8shim/pull/72#discussion_r382853130
########## File path: pkg/appmgmt/sparkoperator/spark.go ########## @@ -0,0 +1,222 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you 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 sparkoperator + +import ( + "github.com/GoogleCloudPlatform/spark-on-k8s-operator/pkg/apis/sparkoperator.k8s.io/v1beta2" + crcClientSet "github.com/GoogleCloudPlatform/spark-on-k8s-operator/pkg/client/clientset/versioned" + crInformers "github.com/GoogleCloudPlatform/spark-on-k8s-operator/pkg/client/informers/externalversions" + "github.com/apache/incubator-yunikorn-k8shim/pkg/appmgmt/interfaces" + "github.com/apache/incubator-yunikorn-k8shim/pkg/client" + "github.com/apache/incubator-yunikorn-k8shim/pkg/common" + "github.com/apache/incubator-yunikorn-k8shim/pkg/common/utils" + "github.com/apache/incubator-yunikorn-k8shim/pkg/log" + "github.com/apache/incubator-yunikorn-scheduler-interface/lib/go/si" + "go.uber.org/zap" + v1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/labels" + k8sCache "k8s.io/client-go/tools/cache" +) + +const ( + LabelAnnotationPrefix = "sparkoperator.k8s.io/" + SparkAppNameLabel = LabelAnnotationPrefix + "app-name" +) + +// implements interfaces#Recoverable, interfaces#AppManager +type Manager struct { + amProtocol interfaces.ApplicationManagementProtocol + apiProvider client.APIProvider + crdInformer k8sCache.SharedIndexInformer + crdInformerFactory crInformers.SharedInformerFactory + stopCh chan struct{} +} + +func New(amProtocol interfaces.ApplicationManagementProtocol, apiProvider client.APIProvider) *Manager { + return &Manager{ + amProtocol: amProtocol, + apiProvider: apiProvider, + stopCh: make(chan struct{}), + } +} + +// this implements AppManagementService interface +func (os *Manager) ServiceInit() error { + crClient, err := crcClientSet.NewForConfig( + os.apiProvider.GetAPIs().KubeClient.GetConfigs()) + if err != nil { + return err + } + + var factoryOpts []crInformers.SharedInformerOption + os.crdInformerFactory = crInformers.NewSharedInformerFactoryWithOptions( + crClient, 0, factoryOpts...) + os.crdInformerFactory.Sparkoperator().V1beta2().SparkApplications().Informer() + os.crdInformer = os.crdInformerFactory.Sparkoperator().V1beta2().SparkApplications().Informer() + os.crdInformer.AddEventHandler(k8sCache.ResourceEventHandlerFuncs{ + AddFunc: os.addApplication, + UpdateFunc: os.updateApplication, + DeleteFunc: os.deleteApplication, + }) + + os.apiProvider.AddEventHandler(&client.ResourceEventHandlers{ + Type: client.PodInformerHandlers, + FilterFn: os.filterPod, + AddFn: os.addPod, + UpdateFn: os.updatePod, + DeleteFn: os.deletePod, + }) + + return nil +} + +func (os *Manager) Name() string { + return "spark-operator-service" +} + +func (os *Manager) Start() error { + log.Logger.Info("starting", zap.String("Name", os.Name())) + go os.crdInformerFactory.Start(os.stopCh) Review comment: if init() failed, start() will not be called. but adding a safe-guard is always good : ). ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
