Files
ubicloud/kubernetes/csi/bin/ubi-csi-server
Daniel Farina 7963a0ec01 Outline mkdir_p for CSI directory initialization
It's unusual to have such a side effect upon object initialization.

In production cases, this object is not created more than once per
startup, so, no big deal...but once testing and mocks get involved, it
clutters the tests, so abide the more conventional approach of
outlining the side effect.

In passing, remove the check for existence of a directory before
`mkdir_p`: it idempotent, it will check if there's nothing to do
internally anyway.
2025-07-25 15:20:55 +02:00

47 lines
1.3 KiB
Ruby
Executable File

#!/usr/bin/env ruby
# frozen_string_literal: true
$LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
require "bundler/setup"
require "ubi_csi"
require "logger"
$stdout.sync = true
logger = Logger.new($stdout)
logger.level = $VERBOSE ? Logger::DEBUG : Logger::INFO
logger.debug("Starting main function")
endpoint = ENV["CSI_ENDPOINT"].dup
raise "CSI_ENDPOINT environment variable is not set" if endpoint.nil?
endpoint.strip!
raise "CSI_ENDPOINT must have unix:// prefix" unless endpoint.start_with?("unix://")
node_id = ENV["NODE_ID"].dup
raise "NODE_ID environment variable is not set" if node_id.nil?
node_id.strip!
raise "NODE_ID cannot be nil" if node_id.empty?
logger.debug("Creating gRPC server")
server = GRPC::RpcServer.new
socket_path = endpoint.sub("unix://", "")
logger.debug("Socket path: #{socket_path}")
File.delete(socket_path) if File.exist?(socket_path)
server.add_http2_port("unix:#{socket_path}", :this_port_is_insecure)
Csi::V1::NodeService.mkdir_p
logger.debug("Registering services")
server.handle(Csi::V1::IdentityService.new(logger:))
server.handle(Csi::V1::ControllerService.new(logger:))
server.handle(Csi::V1::NodeService.new(logger:, node_id:))
logger.debug("Starting UbiCSI server on #{endpoint}")
begin
server.run_till_terminated
rescue => e
warn("Error starting server: #{e.message}\n#{e.backtrace.join("\n")}")
raise
end