|
| 1 | +/** |
| 2 | + * Copyright 2019 IBM Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package driver |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + csi "github.com/container-storage-interface/spec/lib/go/csi" |
| 22 | + util "github.com/ibm/ibm-block-csi-driver/node/util" |
| 23 | + "io/ioutil" |
| 24 | + "net" |
| 25 | + "os" |
| 26 | + |
| 27 | + "google.golang.org/grpc" |
| 28 | + "gopkg.in/yaml.v2" |
| 29 | + "k8s.io/klog" |
| 30 | +) |
| 31 | + |
| 32 | +type Driver struct { |
| 33 | + nodeService |
| 34 | + srv *grpc.Server |
| 35 | + endpoint string |
| 36 | + config ConfigFile |
| 37 | +} |
| 38 | + |
| 39 | +func NewDriver(endpoint string) (*Driver, error) { |
| 40 | + configFile, err := ReadConfigFile() |
| 41 | + if err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + klog.Infof("Driver: %v Version: %v", configFile.Identity.Name, configFile.Identity.Version) |
| 45 | + |
| 46 | + return &Driver{ |
| 47 | + endpoint: endpoint, |
| 48 | + config: configFile, |
| 49 | + nodeService: newNodeService(configFile), |
| 50 | + }, nil |
| 51 | +} |
| 52 | + |
| 53 | +func (d *Driver) Run() error { |
| 54 | + scheme, addr, err := util.ParseEndpoint(d.endpoint) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + listener, err := net.Listen(scheme, addr) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + logErr := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { |
| 65 | + resp, err := handler(ctx, req) |
| 66 | + if err != nil { |
| 67 | + klog.Errorf("GRPC error: %v", err) |
| 68 | + } |
| 69 | + return resp, err |
| 70 | + } |
| 71 | + opts := []grpc.ServerOption{ |
| 72 | + grpc.UnaryInterceptor(logErr), |
| 73 | + } |
| 74 | + d.srv = grpc.NewServer(opts...) |
| 75 | + |
| 76 | + csi.RegisterIdentityServer(d.srv, d) |
| 77 | + csi.RegisterNodeServer(d.srv, d) |
| 78 | + |
| 79 | + klog.Infof("Listening for connections on address: %#v", listener.Addr()) |
| 80 | + return d.srv.Serve(listener) |
| 81 | +} |
| 82 | + |
| 83 | +func (d *Driver) Stop() { |
| 84 | + klog.Infof("Stopping server") |
| 85 | + d.srv.Stop() |
| 86 | +} |
| 87 | + |
| 88 | +type ConfigFile struct { |
| 89 | + Identity struct { |
| 90 | + Name string |
| 91 | + Version string |
| 92 | + // TODO missing capabilities |
| 93 | + } |
| 94 | + Controller struct { |
| 95 | + Publish_context_lun_parameter string |
| 96 | + Publish_context_connectivity_parameter string |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +const ( |
| 101 | + DefualtConfigFile string = "config.yaml" |
| 102 | + EnvNameDriverConfFile string = "DRIVER_CONFIG_YML" |
| 103 | +) |
| 104 | + |
| 105 | +func ReadConfigFile() (ConfigFile, error) { |
| 106 | + var configFile ConfigFile |
| 107 | + |
| 108 | + configYamlPath := os.Getenv(EnvNameDriverConfFile) |
| 109 | + if configYamlPath == "" { |
| 110 | + configYamlPath = DefualtConfigFile |
| 111 | + klog.V(4).Infof("Not found config file environment variable %s. Set default value %s.", EnvNameDriverConfFile, configYamlPath) |
| 112 | + } else { |
| 113 | + klog.V(4).Infof("Config file environment variable %s=%s", EnvNameDriverConfFile, configYamlPath) |
| 114 | + } |
| 115 | + |
| 116 | + yamlFile, err := ioutil.ReadFile(configYamlPath) |
| 117 | + if err != nil { |
| 118 | + klog.Errorf("failed to read file %q: %v", yamlFile, err) |
| 119 | + return ConfigFile{}, err |
| 120 | + } |
| 121 | + |
| 122 | + err = yaml.Unmarshal(yamlFile, &configFile) |
| 123 | + if err != nil { |
| 124 | + klog.Errorf("error unmarshaling yaml: %v", err) |
| 125 | + return ConfigFile{}, err |
| 126 | + } |
| 127 | + |
| 128 | + // Verify mandatory attributes in config file |
| 129 | + if configFile.Identity.Name == "" { |
| 130 | + err := &ConfigYmlEmptyAttribute{"Identity.Name"} |
| 131 | + klog.Errorf("%v", err) |
| 132 | + return ConfigFile{}, err |
| 133 | + } |
| 134 | + |
| 135 | + if configFile.Identity.Version == "" { |
| 136 | + err := &ConfigYmlEmptyAttribute{"Identity.Version"} |
| 137 | + klog.Errorf("%v", err) |
| 138 | + return ConfigFile{}, err |
| 139 | + } |
| 140 | + |
| 141 | + return configFile, nil |
| 142 | +} |
0 commit comments