Skip to content
This repository has been archived by the owner on Oct 5, 2021. It is now read-only.

Commit

Permalink
feat(jiva-csi): add initial code for jiva-csi (#1)
Browse files Browse the repository at this point in the history
This commit adds code to do following
- Create JivaVolume CR with the pv name (watched by jiva-operator) and other
  parameters from storage-class
```
  apiVersion: storage.k8s.io/v1
  kind: StorageClass
  metadata:
    name: openebs-jiva-csi
  provisioner: openebs-jiva-csi.openebs.io
  parameters:
    cas-type: "jiva"
    replicaCount: "1"
    replicaSC: "openebs-hostpath"
    replicaMinCPU: "120m"
    replicaMinMemory: "500m"
    replicaMaxCPU: "1M"
    replicaMaxMemory: "1G"
    targetMinCPU: "120m"
    targetMinMemory: "500m"
    targetMaxCPU: "1M"
    targetMaxMemory: "1G"
```
- Code to perform iscsi login, format and mount

Signed-off-by: Utkarsh Mani Tripathi <[email protected]>
  • Loading branch information
utkarshmani1997 authored and kmova committed Dec 4, 2019
1 parent bd78d41 commit 4f5465a
Show file tree
Hide file tree
Showing 20 changed files with 3,335 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cscope*
build/bin/*
vendor/*
50 changes: 50 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
language: go
dist: xenial
sudo: required
install: true
services:
- docker
go:
- 1.13.x
env:
global:
- GOARCH=$(go env GOARCH)
- GO_FOR_RELEASE=1.13
- CHANGE_MINIKUBE_NONE_USER=true
- MINIKUBE_WANTUPDATENOTIFICATION=false
- MINIKUBE_WANTREPORTERRORPROMPT=false
- MINIKUBE_HOME=$HOME
- CHANGE_MINIKUBE_NONE_USER=true
- KUBECONFIG=$HOME/.kube/config
addons:
apt:
update: true

install:
- make bootstrap

before_install:
# Download kubectl, which is a requirement for using minikube.
#- curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/v1.14.0/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/
# Download minikube.
#- curl -Lo minikube https://storage.googleapis.com/minikube/releases/v1.5.2/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
#- mkdir -p $HOME/.kube $HOME/.minikube
#- touch $KUBECONFIG
#- sudo minikube start --vm-driver=none --kubernetes-version=v1.14.0
#- "sudo chown -R travis: /home/travis/.minikube/"
script:
#- kubectl cluster-info
# Verify kube-addon-manager.
# kube-addon-manager is responsible for managing other kubernetes components, such as kube-dns, dashboard, storage-provisioner..
#- JSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}'; until kubectl -n kube-system get pods -lcomponent=kube-addon-manager -o jsonpath="$JSONPATH" 2>&1 | grep -q "Ready=True"; do sleep 1;echo "waiting for kube-addon-manager to be available"; kubectl get pods --all-namespaces; done
- make image
#- ./ci/travis-ci.sh
after_success:
- make push-tag
notifications:
email:
recipients:
- [email protected]
webhooks:
urls:
- https://webhooks.gitter.im/e/d885a0beef16020c3878
133 changes: 133 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Output registry and image names for operator image
# Set env to override this value
REGISTRY ?= openebs

# Output plugin name and its image name and tag
PLUGIN_NAME=jiva-csi
PLUGIN_TAG=ci

# Tools required for different make targets or for development purposes
EXTERNAL_TOOLS=\
golang.org/x/tools/cmd/cover \
github.com/axw/gocov/gocov \
github.com/ugorji/go/codec/codecgen \

# Lint our code. Reference: https://golang.org/cmd/vet/
VETARGS?=-asmdecl -atomic -bool -buildtags -copylocks -methods \
-nilfunc -printf -rangeloops -shift -structtag -unsafeptr

GIT_BRANCH = $(shell git rev-parse --abbrev-ref HEAD | sed -e "s/.*\\///")
GIT_TAG = $(shell git describe --tags)

# use git branch as default version if not set by env variable, if HEAD is detached that use the most recent tag
VERSION ?= $(if $(subst HEAD,,${GIT_BRANCH}),$(GIT_BRANCH),$(GIT_TAG))
COMMIT ?= $(shell git rev-parse HEAD | cut -c 1-7)

ifeq ($(GIT_TAG),)
GIT_TAG := $(COMMIT)
endif

ifeq (${TRAVIS_TAG}, )
GIT_TAG = $(COMMIT)
export GIT_TAG
else
GIT_TAG = ${TRAVIS_TAG}
export GIT_TAG
endif

PACKAGES = $(shell go list ./... | grep -v 'vendor')

DATETIME ?= $(shell date +'%F_%T')
LDFLAGS ?= \
-extldflags "-static" \
-X github.com/openebs/jiva-csi/version/version.Version=${VERSION} \
-X github.com/openebs/jiva-csi/version/version.Commit=${COMMIT} \
-X github.com/openebs/jiva-csi/version/version.DateTime=${DATETIME}


.PHONY: help
help:
@echo "Available commands:"
@echo " build - build csi source code"
@echo " image - build csi container image"
@echo " push - push csi to dockerhub registry (${REGISTRY})"
@echo ""
@make print-variables --no-print-directory

.PHONY: print-variables
print-variables:
@echo "Variables:"
@echo " VERSION: ${VERSION}"
@echo " GIT_BRANCH: ${GIT_BRANCH}"
@echo " GIT_TAG: ${GIT_TAG}"
@echo " COMMIT: ${COMMIT}"
@echo "Testing variables:"
@echo " Produced Image: ${PLUGIN_NAME}:${PLUGIN_TAG}"
@echo " REGISTRY: ${REGISTRY}"

# Bootstrap the build by downloading additional tools
bootstrap:
@for tool in $(EXTERNAL_TOOLS) ; do \
echo "+ Installing $$tool" ; \
go get -u $$tool; \
done

.get:
rm -rf ./build/bin/
go mod download

vet:
@go vet 2>/dev/null ; if [ $$? -eq 3 ]; then \
go get golang.org/x/tools/cmd/vet; \
fi
@echo "--> Running go tool vet ..."
@go vet $(VETARGS) ${PACKAGES} ; if [ $$? -eq 1 ]; then \
echo ""; \
echo "[LINT] Vet found suspicious constructs. Please check the reported constructs"; \
echo "and fix them if necessary before submitting the code for review."; \
fi

@git grep -n `echo "log"".Print"` | grep -v 'vendor/' ; if [ $$? -eq 0 ]; then \
echo "[LINT] Found "log"".Printf" calls. These should use Maya's logger instead."; \
fi


deps: .get
go mod vendor

build: deps test
@echo "--> Build binary $(PLUGIN_NAME) ..."
GOOS=linux go build -a -ldflags '$(LDFLAGS)' -o ./build/bin/$(PLUGIN_NAME) ./cmd/csi/main.go

image: build
@echo "--> Build image $(REGISTRY)/$(PLUGIN_NAME):$(PLUGIN_TAG) ..."
docker build -f ./build/Dockerfile -t $(REGISTRY)/$(PLUGIN_NAME):$(PLUGIN_TAG) .

push-image: image
@echo "--> Push image $(REGISTRY)/$(PLUGIN_NAME):$(PLUGIN_TAG) ..."
docker push $(REGISTRY)/$(PLUGIN_NAME):$(PLUGIN_TAG)

push:
@echo "--> Push image $(REGISTRY)/$(PLUGIN_NAME):$(PLUGIN_TAG) ..."
docker push $(REGISTRY)/$(PLUGIN_NAME):$(PLUGIN_TAG)

tag:
@echo "--> Tag image $(REGISTRY)/$(PLUGIN_NAME):$(PLUGIN_TAG) to $(REGISTRY)/$(PLUGIN_NAME):$(GIT_TAG) ..."
docker tag $(REGISTRY)/$(PLUGIN_NAME):$(PLUGIN_TAG) $(REGISTRY)/$(PLUGIN_NAME):$(GIT_TAG)

push-tag: tag push
@echo "--> Push image $(REGISTRY)/$(PLUGIN_NAME):$(GIT_TAG) ..."
docker push $(REGISTRY)/$(PLUGIN_NAME):$(GIT_TAG)

clean:
rm -rf ./build/bin/
go mod tidy

format:
@echo "--> Running go fmt"
@go fmt $(PACKAGES)

test: format vet clean
@echo "--> Running go test" ;
@go test -v --cover $(PACKAGES)

8 changes: 8 additions & 0 deletions build/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM ubuntu:16.04
RUN apt-get update; exit 0
RUN apt-get -y install rsyslog xfsprogs
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

COPY build/bin/jiva-csi /usr/local/bin/

ENTRYPOINT ["/usr/local/bin/jiva-csi"]
151 changes: 151 additions & 0 deletions cmd/csi/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
Copyright © 2018-2019 The OpenEBS 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 (
"flag"
"fmt"
"log"
"os"

"github.com/kubernetes-csi/csi-lib-iscsi/iscsi"
"github.com/openebs/jiva-csi/pkg/config"
"github.com/openebs/jiva-csi/pkg/driver"
"github.com/openebs/jiva-csi/pkg/kubernetes/client"
"github.com/openebs/jiva-csi/version"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/klog"
k8scfg "sigs.k8s.io/controller-runtime/pkg/client/config"
)

// log2LogrusWriter implement io.Writer interface used to enable
// debug logs for iscsi lib
type log2LogrusWriter struct {
entry *logrus.Entry
}

// Write redirects the std log to logrus
func (w *log2LogrusWriter) Write(b []byte) (int, error) {
n := len(b)
if n > 0 && b[n-1] == '\n' {
b = b[:n-1]
}
w.entry.Debug(string(b))
return n, nil
}

var enableISCSIDebug bool

/*
* main routine to start the jiva-csi-driver. The same
* binary is used for controller and agent deployment.
* they both are differentiated via plugin command line
* argument. To start the controller, we have to pass
* --plugin=controller and to start it as node, we have
* to pass --plugin=node.
*/
func main() {
var config = config.Default()
// initializing klog for the kubernetes libraries used
klog.InitFlags(nil)
cmd := &cobra.Command{
Use: "jiva-csi-driver",
Short: "driver for provisioning jiva volume",
Long: `provisions and deprovisions the volume`,
Run: func(cmd *cobra.Command, args []string) {
run(config)
},
}

cmd.Flags().AddGoFlagSet(flag.CommandLine)
_ = flag.CommandLine.Parse([]string{})

cmd.PersistentFlags().StringVar(
&config.NodeID, "nodeid", "", "NodeID to identify the node running this driver",
)

cmd.PersistentFlags().StringVar(
&config.Version, "version", version.Version, "Displays driver version",
)

cmd.PersistentFlags().StringVar(
&config.Endpoint, "endpoint", "unix:///plugin/csi.sock", "CSI endpoint",
)

cmd.PersistentFlags().StringVar(
&config.DriverName, "name", "jiva.csi.openebs.io", "Name of this driver",
)

cmd.PersistentFlags().StringVar(
&config.PluginType, "plugin", "", "Type of this driver i.e. controller or node",
)

cmd.Flags().BoolVar(
&enableISCSIDebug, "enableiscsidebug", false, "Enable iscsi debug logs",
)

err := cmd.Execute()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s", err.Error())
os.Exit(1)
}
}

func run(config *config.Config) {
if config.Version == "" {
config.Version = version.Version
}

logrus.Infof("%s - %s", version.Version, version.Commit)
logrus.Infof(
"DriverName: %s Plugin: %s EndPoint: %s NodeID: %s",
config.DriverName,
config.PluginType,
config.Endpoint,
config.NodeID,
)

if config.PluginType == "node" && enableISCSIDebug {
logrus.SetLevel(logrus.DebugLevel)
iscsi.EnableDebugLogging(&log2LogrusWriter{
entry: logrus.StandardLogger().WithField("logger", "iscsi"),
})
}

// get the kube config
cfg, err := k8scfg.GetConfig()
if err != nil {
logrus.Fatalf("error getting config: %v", err)
}

// generate a new client object
cli, err := client.New(cfg)
if err != nil {
logrus.Fatalf("error creating client from config: %v", err)
}

if err := cli.RegisterAPI(); err != nil {
logrus.Fatalf("error registering API: %v", err)
}

err = driver.New(config, cli).Run()
if err != nil {
log.Fatalln(err)
}
os.Exit(0)
}
Loading

0 comments on commit 4f5465a

Please sign in to comment.