This is an automated email from the ASF dual-hosted git repository.

kaihsun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/submarine.git


The following commit(s) were added to refs/heads/master by this push:
     new 95651d8  SUBMARINE-783. Run submarine-operator in k8s cluster
95651d8 is described below

commit 95651d8cdf61e3215144c28b74f098d35613aa57
Author: kevin85421 <[email protected]>
AuthorDate: Wed Apr 7 14:28:19 2021 +0800

    SUBMARINE-783. Run submarine-operator in k8s cluster
    
    ### What is this PR for?
    Run submarine-operator in k8s cluster. To achieve this, we need to
    
    (1) define appropriate ClusterRole, ClusterRoleBinding, and ServiceAccount
    (2) Dockerfile
    (3) submarine-operator deployment YAML file
    
    
    ### What type of PR is it?
    [Feature]
    
    ### Todos
    * Optimize Dockerfile
    * Optimize ClusterRole configuration
    
    ### What is the Jira issue?
    https://issues.apache.org/jira/browse/SUBMARINE-783
    
    ### How should this be tested?
    
    
https://user-images.githubusercontent.com/20109646/113503183-c8047100-9562-11eb-844d-95f725f48ea6.mov
    
    
    ### Screenshots (if appropriate)
    
    ### Questions:
    * Does the licenses files need update? No
    * Is there breaking changes for older versions? No
    * Does this needs documentation? Yes
---
 submarine-cloud-v2/Dockerfile                      | 31 ++++++++++++
 submarine-cloud-v2/Makefile                        | 27 ++++++++++
 submarine-cloud-v2/README.md                       | 22 ++++++--
 .../submarine-operator-service-account.yaml        | 59 ++++++++++++++++++++++
 .../artifacts/examples/submarine-operator.yaml     | 43 ++++++++++++++++
 submarine-cloud-v2/go.mod                          |  1 +
 submarine-cloud-v2/main.go                         | 15 +++++-
 7 files changed, 193 insertions(+), 5 deletions(-)

diff --git a/submarine-cloud-v2/Dockerfile b/submarine-cloud-v2/Dockerfile
new file mode 100644
index 0000000..cb67e2c
--- /dev/null
+++ b/submarine-cloud-v2/Dockerfile
@@ -0,0 +1,31 @@
+# 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.
+
+FROM ubuntu:18.04
+MAINTAINER Apache Software Foundation <[email protected]>
+
+RUN apt-get update &&\
+    apt-get install -y wget vim git
+
+ENV GOROOT="/usr/local/go"
+ENV GOPATH=$HOME/gocode
+ENV GOBIN=$GOPATH/bin
+ENV PATH=$PATH:$GOPATH:$GOBIN:$GOROOT/bin
+
+RUN wget https://dl.google.com/go/go1.16.2.linux-amd64.tar.gz &&\
+    tar -C /usr/local -xzf go1.16.2.linux-amd64.tar.gz
+
+ADD submarine-operator /usr/src
+CMD ["/usr/src/submarine-operator", "-incluster=true"] 
diff --git a/submarine-cloud-v2/Makefile b/submarine-cloud-v2/Makefile
new file mode 100644
index 0000000..7dac348
--- /dev/null
+++ b/submarine-cloud-v2/Makefile
@@ -0,0 +1,27 @@
+#
+# 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.
+#
+
+.PHONY: image
+image:
+       GOOS=linux go build -o submarine-operator
+       docker build -t submarine-operator .
+       go build -o submarine-operator
+       touch $@
+
+.PHONY: test-unit
+test-unit:
+       go test
diff --git a/submarine-cloud-v2/README.md b/submarine-cloud-v2/README.md
index 7490e4a..2c7c083 100644
--- a/submarine-cloud-v2/README.md
+++ b/submarine-cloud-v2/README.md
@@ -37,11 +37,27 @@ kubectl apply -f artifacts/examples/crd.yaml
 kubectl apply -f artifacts/examples/example-submarine.yaml
 
 # Step3: Run unit test
-go test
+make test-unit
 ```
 
-# Build Project
+# Run submarine-operator out-of-cluster
 ```bash
 go build -o submarine-operator
 ./submarine-operator
-```
\ No newline at end of file
+```
+
+# Run operator in-cluster
+```bash
+# Step1: Build image "submarine-operator" to minikube's Docker 
+eval $(minikube docker-env)
+make image
+
+# Step2: RBAC (ClusterRole, ClusterRoleBinding, and ServiceAccount)
+kubectl apply -f artifacts/examples/submarine-operator-service-account.yaml
+
+# Step3: Deploy a submarine-operator
+kubectl apply -f artifacts/examples/submarine-operator.yaml
+
+# Step4: Inspect submarine-operator POD logs 
+kubectl logs ${submarine-operator POD}
+```
diff --git 
a/submarine-cloud-v2/artifacts/examples/submarine-operator-service-account.yaml 
b/submarine-cloud-v2/artifacts/examples/submarine-operator-service-account.yaml
new file mode 100644
index 0000000..913e6c7
--- /dev/null
+++ 
b/submarine-cloud-v2/artifacts/examples/submarine-operator-service-account.yaml
@@ -0,0 +1,59 @@
+#
+# 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.
+#
+
+kind: ClusterRole
+apiVersion: rbac.authorization.k8s.io/v1
+metadata:
+  name: submarine-operator 
+rules:
+- apiGroups: 
+  - submarine.k8s.io
+  resources:
+  - submarines
+  verbs:
+  - '*'
+- apiGroups:
+  - ""
+  resources:
+  - pods
+  verbs:
+  - '*'
+- apiGroups:
+  - "apps"
+  resources:
+  - deployments
+  verbs:
+  - '*'
+---
+apiVersion: v1
+kind: ServiceAccount
+metadata:
+  name: submarine-operator
+  namespace: default
+---
+apiVersion: rbac.authorization.k8s.io/v1
+kind: ClusterRoleBinding
+metadata:
+  name: submarine-operator
+roleRef:
+  apiGroup: rbac.authorization.k8s.io
+  kind: ClusterRole
+  name: submarine-operator
+subjects:
+- kind: ServiceAccount
+  name: submarine-operator
+  namespace: default
\ No newline at end of file
diff --git a/submarine-cloud-v2/artifacts/examples/submarine-operator.yaml 
b/submarine-cloud-v2/artifacts/examples/submarine-operator.yaml
new file mode 100644
index 0000000..11a6a25
--- /dev/null
+++ b/submarine-cloud-v2/artifacts/examples/submarine-operator.yaml
@@ -0,0 +1,43 @@
+#
+# 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.
+#
+
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+  creationTimestamp: null
+  labels:
+    app: submarine-operator-demo
+  name: submarine-operator-demo
+spec:
+  replicas: 1
+  selector:
+    matchLabels:
+      app: submarine-operator-demo
+  strategy: {}
+  template:
+    metadata:
+      creationTimestamp: null
+      labels:
+        app: submarine-operator-demo
+    spec:
+      containers:
+      - image: submarine-operator
+        name: submarine-operator
+        resources: {}
+        imagePullPolicy: Never
+      serviceAccountName: submarine-operator
+status: {}
diff --git a/submarine-cloud-v2/go.mod b/submarine-cloud-v2/go.mod
index 0f87014..6169426 100644
--- a/submarine-cloud-v2/go.mod
+++ b/submarine-cloud-v2/go.mod
@@ -4,6 +4,7 @@ go 1.16
 
 require (
        gonum.org/v1/netlib v0.0.0-20190331212654-76723241ea4e // indirect
+       k8s.io/api v0.20.4
        k8s.io/apimachinery v0.20.4
        k8s.io/client-go v0.20.4
        k8s.io/code-generator v0.20.4
diff --git a/submarine-cloud-v2/main.go b/submarine-cloud-v2/main.go
index b09ebea..1d94803 100644
--- a/submarine-cloud-v2/main.go
+++ b/submarine-cloud-v2/main.go
@@ -24,6 +24,7 @@ import (
        "k8s.io/klog/v2"
        "k8s.io/client-go/tools/clientcmd"
        "k8s.io/client-go/kubernetes"
+       "k8s.io/client-go/rest"
        "os"
        "submarine-cloud-v2/pkg/signals"
        clientset "submarine-cloud-v2/pkg/generated/clientset/versioned"
@@ -33,8 +34,16 @@ import (
 var (
        masterURL  string
        kubeconfig string
+       incluster  bool
 )
 
+func initKubeConfig() (*rest.Config, error) {
+       if !incluster {
+               return clientcmd.BuildConfigFromFlags(masterURL, kubeconfig) // 
out-of-cluster config
+       }
+       return rest.InClusterConfig() // in-cluster config
+}
+
 func main() {
        klog.InitFlags(nil)
        flag.Parse()
@@ -42,7 +51,8 @@ func main() {
        // set up signals so we handle the first shutdown signal gracefully
        stopCh := signals.SetupSignalHandler()
 
-       cfg, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfig)
+       cfg, err := initKubeConfig()
+       
        if err != nil {
                klog.Fatalf("Error building kubeconfig: %s", err.Error())
        }
@@ -80,6 +90,7 @@ func main() {
 }
 
 func init() {
+       flag.BoolVar(&incluster, "incluster", false, "Run submarine-operator 
in-cluster")
        flag.StringVar(&kubeconfig, "kubeconfig", os.Getenv("HOME") + 
"/.kube/config", "Path to a kubeconfig. Only required if out-of-cluster.")
        flag.StringVar(&masterURL, "master", "", "The address of the Kubernetes 
API server. Overrides any value in kubeconfig. Only required if 
out-of-cluster.")
-}
\ No newline at end of file
+}

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to