2018-03-05 09:00:23 +00:00
|
|
|
//
|
|
|
|
// DISCLAIMER
|
|
|
|
//
|
|
|
|
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// Copyright holder is ArangoDB GmbH, Cologne, Germany
|
|
|
|
//
|
|
|
|
// Author Ewout Prangsma
|
|
|
|
//
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
goflag "flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/arangodb/k8s-operator/pkg/logging"
|
2018-03-05 10:06:53 +00:00
|
|
|
"github.com/arangodb/k8s-operator/pkg/storage/provisioner/service"
|
2018-03-05 09:00:23 +00:00
|
|
|
"github.com/arangodb/k8s-operator/pkg/util/constants"
|
|
|
|
"github.com/arangodb/k8s-operator/pkg/util/k8sutil"
|
|
|
|
)
|
|
|
|
|
2018-03-05 10:06:53 +00:00
|
|
|
const (
|
|
|
|
defaultProvisionerPort = 8929
|
|
|
|
)
|
|
|
|
|
2018-03-05 09:00:23 +00:00
|
|
|
var (
|
|
|
|
cmdStorage = &cobra.Command{
|
|
|
|
Use: "storage",
|
|
|
|
Run: cmdUsage,
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdStorageProvisioner = &cobra.Command{
|
|
|
|
Use: "provisioner",
|
|
|
|
Run: cmdStorageProvisionerRun,
|
|
|
|
}
|
|
|
|
|
|
|
|
storageProvisioner struct {
|
2018-03-05 10:06:53 +00:00
|
|
|
port int
|
2018-03-05 09:00:23 +00:00
|
|
|
localPath []string
|
|
|
|
storageClassName string
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmdMain.AddCommand(cmdStorage)
|
|
|
|
cmdStorage.AddCommand(cmdStorageProvisioner)
|
|
|
|
|
|
|
|
f := cmdStorageProvisioner.Flags()
|
|
|
|
f.StringSliceVar(&storageProvisioner.localPath, "local-path", nil, "Local directory to provision volumes into")
|
|
|
|
f.StringVar(&storageProvisioner.storageClassName, "storage-class-name", "", "StorageClassName set in provisioned volumes")
|
2018-03-05 10:06:53 +00:00
|
|
|
f.IntVar(&storageProvisioner.port, "port", defaultProvisionerPort, "Port to listen on")
|
2018-03-05 09:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the provisioner
|
|
|
|
func cmdStorageProvisionerRun(cmd *cobra.Command, args []string) {
|
|
|
|
goflag.CommandLine.Parse([]string{"-logtostderr"})
|
|
|
|
var err error
|
|
|
|
logService, err = logging.NewService(logLevel)
|
|
|
|
if err != nil {
|
|
|
|
cliLog.Fatal().Err(err).Msg("Failed to initialize log service")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log version
|
|
|
|
cliLog.Info().Msgf("Starting arangodb local storage provisioner, version %s build %s", projectVersion, projectBuild)
|
|
|
|
|
|
|
|
// Get environment
|
|
|
|
namespace := os.Getenv(constants.EnvOperatorPodNamespace)
|
|
|
|
if len(namespace) == 0 {
|
|
|
|
cliLog.Fatal().Msgf("%s environment variable missing", constants.EnvOperatorPodNamespace)
|
|
|
|
}
|
|
|
|
name := os.Getenv(constants.EnvOperatorPodName)
|
|
|
|
if len(name) == 0 {
|
|
|
|
cliLog.Fatal().Msgf("%s environment variable missing", constants.EnvOperatorPodName)
|
|
|
|
}
|
|
|
|
nodeName := os.Getenv(constants.EnvOperatorNodeName)
|
|
|
|
if len(name) == 0 {
|
|
|
|
cliLog.Fatal().Msgf("%s environment variable missing", constants.EnvOperatorNodeName)
|
|
|
|
}
|
|
|
|
|
|
|
|
config, deps, err := newProvisionerConfigAndDeps(nodeName, namespace, name)
|
|
|
|
if err != nil {
|
|
|
|
cliLog.Fatal().Err(err).Msg("Failed to create provisioner config & dependencies")
|
|
|
|
}
|
2018-03-05 10:06:53 +00:00
|
|
|
p, err := service.New(config, deps)
|
2018-03-05 09:00:23 +00:00
|
|
|
if err != nil {
|
|
|
|
cliLog.Fatal().Err(err).Msg("Failed to create provisioner")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
p.Run(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newProvisionerConfigAndDeps creates storage provisioner config & dependencies.
|
2018-03-05 10:06:53 +00:00
|
|
|
func newProvisionerConfigAndDeps(nodeName, namespace, name string) (service.Config, service.Dependencies, error) {
|
2018-03-05 09:00:23 +00:00
|
|
|
kubecli, err := k8sutil.NewKubeClient()
|
|
|
|
if err != nil {
|
2018-03-05 10:06:53 +00:00
|
|
|
return service.Config{}, service.Dependencies{}, maskAny(err)
|
2018-03-05 09:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
serviceAccount, err := getMyPodServiceAccount(kubecli, namespace, name)
|
|
|
|
if err != nil {
|
2018-03-05 10:06:53 +00:00
|
|
|
return service.Config{}, service.Dependencies{}, maskAny(fmt.Errorf("Failed to get my pod's service account: %s", err))
|
2018-03-05 09:00:23 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 10:06:53 +00:00
|
|
|
cfg := service.Config{
|
2018-03-05 09:00:23 +00:00
|
|
|
LocalPath: storageProvisioner.localPath,
|
|
|
|
NodeName: nodeName,
|
|
|
|
Namespace: namespace,
|
|
|
|
ServiceAccount: serviceAccount,
|
|
|
|
}
|
2018-03-05 10:06:53 +00:00
|
|
|
deps := service.Dependencies{
|
2018-03-05 09:00:23 +00:00
|
|
|
Log: logService.MustGetLogger("provisioner"),
|
|
|
|
KubeCli: kubecli,
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg, deps, nil
|
|
|
|
}
|