Skip to content

Commit

Permalink
Add basic project layout
Browse files Browse the repository at this point in the history
Signed-off-by: Yury Kulazhenkov <[email protected]>
  • Loading branch information
ykulazhenkov committed Sep 6, 2023
1 parent 37eee30 commit ccab966
Show file tree
Hide file tree
Showing 14 changed files with 656 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
bin
build/
testbin/*
.gocache
.task

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
*.cover

# Kubernetes Generated files - skip generated files, except for vendored files

!vendor/**/zz_generated.*

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work

# editor and IDE
.idea
.vscode
*.swp
*.swo
*~
26 changes: 26 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Build the image
FROM golang:1.20 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN go mod download
RUN go install github.com/go-task/task/v3/cmd/[email protected]


# Copy the go source
COPY . /workspace

# Build with make to apply all build logic defined in Makefile
RUN task build

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM gcr.io/distroless/static-debian11:latest
WORKDIR /
COPY --from=builder /workspace/build/network-operator-init-container .

ENTRYPOINT [ "/network-operator-init-container" ]
79 changes: 79 additions & 0 deletions Taskfile.dist.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
version: "3"

set: [pipefail, e]

shopt: [globstar, nullglob]

output:
group:
begin: '::group::{{.TASK}}'
end: '::endgroup::'

vars:
# Proj related vars
PROJECT_DIR: "{{.USER_WORKING_DIR}}"
BUILD_DIR: "{{.PROJECT_DIR}}/build"
LOCAL_BIN: "{{.PROJECT_DIR}}/bin"
# Image related vars
IMAGE_REGISTRY: "ghcr.io/mellanox"
IMAGE_REPOSITORY: "network-operator-init-container"
IMAGE_TAG: "latest"
IMAGE_NAME_FULL: "{{.IMAGE_REGISTRY}}/{{.IMAGE_REPOSITORY}}:{{.IMAGE_TAG}}"
# Coverage related vars
COVER_PROFILE: "{{.PROJECT_DIR}}/network-operator-init-container.cover"

includes:
version: ./taskfiles/Version.yaml
install:
taskfile: ./taskfiles/InstallDeps.yaml
vars:
BIN_DIR: "{{.LOCAL_BIN}}"
kind: ./taskfiles/Kind.yaml
image: ./taskfiles/Image.yaml

tasks:
clean:
desc: remove downloaded tools and compiled binaries
cmd: |
rm -rf {{.LOCAL_BIN}}
rm -rf {{.BUILD_DIR}}
rm -f {{.COVER_PROFILE}}
create-dirs:
desc: prepare build related directories
internal: true
cmds:
- mkdir -p {{.LOCAL_BIN}}
- mkdir -p {{.BUILD_DIR}}
status:
- test -d {{.LOCAL_BIN}}
- test -d {{.BUILD_DIR}}

build:
desc: build network-operator-init-container binary
deps:
- task: create-dirs
vars:
GO_BUILD_OPTS: "CGO_ENABLED=0 GOOS={{OS}} GOARCH={{ARCH}}"
cmd: |
{{.GO_BUILD_OPTS}} go build -ldflags "{{.VERSION_LDFLAGS}}" -o {{.BUILD_DIR}}/network-operator-init-container ./cmd/network-operator-init-container/main.go
sources:
- cmd/**/*.go
- pkg/**/*.go
generates:
- "{{.BUILD_DIR}}/network-operator-init-container"

test:
desc: run unit tests
vars:
COVER_MODE: atomic
GO_PKGS:
sh: go list ./... | grep -v ".*/mocks"
cmd: |
go test -covermode={{.COVER_MODE}} -coverprofile={{.COVER_PROFILE}} {{.GO_PKGS | catLines}}
lint:
desc: run lint tests
deps:
- install:golangci-lint
cmd: "{{.LOCAL_BIN}}/golangci-lint run --timeout 10m"
84 changes: 84 additions & 0 deletions cmd/network-operator-init-container/app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright 2023, NVIDIA CORPORATION & AFFILIATES
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 app

import (
"context"
"fmt"

"github.com/go-logr/logr"
"github.com/spf13/cobra"
cliflag "k8s.io/component-base/cli/flag"
"k8s.io/component-base/term"
"k8s.io/klog/v2"

// register json format for logger
_ "k8s.io/component-base/logs/json/register"

"github.com/Mellanox/network-operator-init-container/pkg/utils/signals"
"github.com/Mellanox/network-operator-init-container/pkg/utils/version"

"github.com/Mellanox/network-operator-init-container/cmd/network-operator-init-container/app/options"
)

// NewNetworkOperatorInitContainerCommand creates a new command
func NewNetworkOperatorInitContainerCommand() *cobra.Command {
opts := options.New()
ctx := signals.SetupShutdownSignals()

cmd := &cobra.Command{
Use: "network-operator-init-container",
Long: `NVIDIA Network Operator init container`,
SilenceUsage: true,
Version: version.GetVersionString(),
RunE: func(cmd *cobra.Command, args []string) error {
if err := opts.Validate(); err != nil {
return fmt.Errorf("invalid config: %w", err)
}
klog.EnableContextualLogging(true)

return RunNetworkOperatorInitContainer(klog.NewContext(ctx, klog.NewKlogr()), opts)
},
Args: func(cmd *cobra.Command, args []string) error {
for _, arg := range args {
if len(arg) > 0 {
return fmt.Errorf("%q does not take any arguments, got %q", cmd.CommandPath(), args)
}
}

return nil
},
}

sharedFS := cliflag.NamedFlagSets{}
opts.AddNamedFlagSets(&sharedFS)

cmdFS := cmd.PersistentFlags()
for _, f := range sharedFS.FlagSets {
cmdFS.AddFlagSet(f)
}

cols, _, _ := term.TerminalSize(cmd.OutOrStdout())
cliflag.SetUsageAndHelpFunc(cmd, sharedFS, cols)

return cmd
}

// RunNetworkOperatorInitContainer runs init container main loop
func RunNetworkOperatorInitContainer(ctx context.Context, opts *options.Options) error {
logger := logr.FromContextOrDiscard(ctx)
logger.Info("start network-operator-init-container", "Options", opts)

return nil
}
55 changes: 55 additions & 0 deletions cmd/network-operator-init-container/app/options/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2023, NVIDIA CORPORATION & AFFILIATES
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 options

import (
"fmt"

cliflag "k8s.io/component-base/cli/flag"
"k8s.io/component-base/logs"
logsapi "k8s.io/component-base/logs/api/v1"
)

// New creates new Options
func New() *Options {
return &Options{
LogConfig: logsapi.NewLoggingConfiguration(),
}
}

// Options contains application options
type Options struct {
LogConfig *logsapi.LoggingConfiguration
}

// AddNamedFlagSets returns FlagSet for Options
func (o *Options) AddNamedFlagSets(sharedFS *cliflag.NamedFlagSets) {
logFS := sharedFS.FlagSet("Logging")
logsapi.AddFlags(o.LogConfig, logFS)
logs.AddFlags(logFS, logs.SkipLoggingConfigurationFlags())

generalFS := sharedFS.FlagSet("General")
_ = generalFS.Bool("version", false, "print version and exit")
_ = generalFS.BoolP("help", "h", false, "print help and exit")
}

// Validate registered options
func (o *Options) Validate() error {
var err error

if err = logsapi.ValidateAndApply(o.LogConfig, nil); err != nil {
return fmt.Errorf("failed to validate logging flags. %w", err)
}
return err
}
26 changes: 26 additions & 0 deletions cmd/network-operator-init-container/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2023, NVIDIA CORPORATION & AFFILIATES
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 (
"os"

"k8s.io/component-base/cli"

"github.com/Mellanox/network-operator-init-container/cmd/network-operator-init-container/app"
)

func main() {
os.Exit(cli.Run(app.NewNetworkOperatorInitContainerCommand()))
}
47 changes: 47 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module github.com/Mellanox/network-operator-init-container

go 1.20

require (
github.com/go-logr/logr v1.2.4
github.com/spf13/cobra v1.7.0
k8s.io/component-base v0.28.1
k8s.io/klog/v2 v2.100.1
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/go-logr/zapr v1.2.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.4 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/net v0.13.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/apimachinery v0.28.1 // indirect
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)
Loading

0 comments on commit ccab966

Please sign in to comment.