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

Commit

Permalink
feat: add feature flag support to go builds
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgacsal committed Feb 15, 2024
1 parent b7753ef commit dae52dc
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 41 deletions.
3 changes: 2 additions & 1 deletion Dockerfile.apiserver
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ RUN --mount=type=cache,target=/go/pkg/mod \
ARG VERSION
ARG BUILD_TIMESTAMP
ARG COMMIT_HASH
ARG BUILD_OPTS

ENV CGO_ENABLED=1

RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
--mount=type=bind,source=.,target=/build,ro \
xx-go build -ldflags="-s -w -extldflags -static \
xx-go build ${BUILD_OPTS} -ldflags="-s -w -extldflags -static \
-X 'github.com/openclarity/vmclarity/core/version.Version=${VERSION}' \
-X 'github.com/openclarity/vmclarity/core/version.CommitHash=${COMMIT_HASH}' \
-X 'github.com/openclarity/vmclarity/core/version.BuildTimestamp=${BUILD_TIMESTAMP}'" \
Expand Down
5 changes: 3 additions & 2 deletions Dockerfile.cli
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ RUN --mount=type=cache,target=/go/pkg/mod \
ARG VERSION
ARG BUILD_TIMESTAMP
ARG COMMIT_HASH
ARG BUILD_OPTS

ENV CGO_ENABLED=0
ENV CGO_ENABLED=1

RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
--mount=type=bind,source=.,target=/build,ro \
xx-go build -ldflags="-s -w -extldflags -static \
xx-go build ${BUILD_OPTS} -ldflags="-s -w -extldflags -static \
-X 'github.com/openclarity/vmclarity/core/version.Version=${VERSION}' \
-X 'github.com/openclarity/vmclarity/core/version.CommitHash=${COMMIT_HASH}' \
-X 'github.com/openclarity/vmclarity/core/version.BuildTimestamp=${BUILD_TIMESTAMP}'" \
Expand Down
5 changes: 3 additions & 2 deletions Dockerfile.cr-discovery-server
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ RUN --mount=type=cache,target=/go/pkg/mod \
ARG VERSION
ARG BUILD_TIMESTAMP
ARG COMMIT_HASH
ARG BUILD_OPTS

ENV CGO_ENABLED=0
ENV CGO_ENABLED=1

RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
--mount=type=bind,source=.,target=/build,ro \
xx-go build -ldflags="-s -w -extldflags -static \
xx-go build ${BUILD_OPTS} -ldflags="-s -w -extldflags -static \
-X 'github.com/openclarity/vmclarity/core/version.Version=${VERSION}' \
-X 'github.com/openclarity/vmclarity/core/version.CommitHash=${COMMIT_HASH}' \
-X 'github.com/openclarity/vmclarity/core/version.BuildTimestamp=${BUILD_TIMESTAMP}'" \
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile.orchestrator
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ RUN --mount=type=cache,target=/go/pkg/mod \
ARG VERSION
ARG BUILD_TIMESTAMP
ARG COMMIT_HASH
ARG BUILD_OPTS

ENV CGO_ENABLED=1

RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
--mount=type=bind,source=.,target=/build,ro \
xx-go build -ldflags="-s -w -extldflags -static \
xx-go build ${BUILD_OPTS} -ldflags="-s -w -extldflags -static \
-X 'github.com/openclarity/vmclarity/core/version.Version=${VERSION}' \
-X 'github.com/openclarity/vmclarity/core/version.CommitHash=${COMMIT_HASH}' \
-X 'github.com/openclarity/vmclarity/core/version.BuildTimestamp=${BUILD_TIMESTAMP}'" \
Expand Down
5 changes: 3 additions & 2 deletions Dockerfile.uibackend
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ RUN --mount=type=cache,target=/go/pkg/mod \
ARG VERSION
ARG BUILD_TIMESTAMP
ARG COMMIT_HASH
ARG BUILD_OPTS

ENV CGO_ENABLED=0
ENV CGO_ENABLED=1

RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
--mount=type=bind,source=.,target=/build,ro \
xx-go build -ldflags="-s -w -extldflags -static \
xx-go build ${BUILD_OPTS} -ldflags="-s -w -extldflags -static \
-X 'github.com/openclarity/vmclarity/core/version.Version=${VERSION}' \
-X 'github.com/openclarity/vmclarity/core/version.CommitHash=${COMMIT_HASH}' \
-X 'github.com/openclarity/vmclarity/core/version.BuildTimestamp=${BUILD_TIMESTAMP}'" \
Expand Down
17 changes: 12 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ DOCKER_REGISTRY ?= ghcr.io/openclarity
DOCKER_TAG ?= $(VERSION)
VMCLARITY_TOOLS_BASE ?=
GO_VERSION ?= $(shell cat $(ROOT_DIR)/.go-version)
GO_BUILD_TAGS ?=

####
## Runtime variables
Expand Down Expand Up @@ -56,25 +57,30 @@ build: ui build-all-go ## Build all components
.PHONY: build-all-go
build-all-go: bin/vmclarity-apiserver bin/vmclarity-cli bin/vmclarity-orchestrator bin/vmclarity-ui-backend bin/vmclarity-cr-discovery-server ## Build all go components

BUILD_OPTS = -race
ifneq ($(GO_BUILD_TAGS),)
BUILD_OPTS += -tags $(GO_BUILD_TAGS)
endif

LDFLAGS = -s -w
LDFLAGS += -X 'github.com/openclarity/vmclarity/core/version.Version=$(VERSION)'
LDFLAGS += -X 'github.com/openclarity/vmclarity/core/version.CommitHash=$(COMMIT_HASH)'
LDFLAGS += -X 'github.com/openclarity/vmclarity/core/version.BuildTimestamp=$(BUILD_TIMESTAMP)'

bin/vmclarity-orchestrator: $(shell find api provider orchestrator utils core) | $(BIN_DIR)
cd orchestrator && go build -race -ldflags="$(LDFLAGS)" -o $(ROOT_DIR)/$@ cmd/main.go
cd orchestrator && go build $(BUILD_OPTS) -ldflags="$(LDFLAGS)" -o $(ROOT_DIR)/$@ cmd/main.go

bin/vmclarity-apiserver: $(shell find api api/server) | $(BIN_DIR)
cd api/server && go build -race -ldflags="$(LDFLAGS)" -o $(ROOT_DIR)/$@ cmd/main.go
cd api/server && go build $(BUILD_OPTS) -ldflags="$(LDFLAGS)" -o $(ROOT_DIR)/$@ cmd/main.go

bin/vmclarity-cli: $(shell find api cli utils core) | $(BIN_DIR)
cd cli && go build -race -ldflags="$(LDFLAGS)" -o $(ROOT_DIR)/$@ cmd/main.go
cd cli && go build $(BUILD_OPTS) -ldflags="$(LDFLAGS)" -o $(ROOT_DIR)/$@ cmd/main.go

bin/vmclarity-ui-backend: $(shell find api uibackend/server) | $(BIN_DIR)
cd uibackend/server && go build -race -ldflags="$(LDFLAGS)" -o $(ROOT_DIR)/$@ cmd/main.go
cd uibackend/server && go build $(BUILD_OPTS) -ldflags="$(LDFLAGS)" -o $(ROOT_DIR)/$@ cmd/main.go

bin/vmclarity-cr-discovery-server: $(shell find api containerruntimediscovery/server utils core) | $(BIN_DIR)
cd containerruntimediscovery/server && go build -race -ldflags="$(LDFLAGS)" -o $(ROOT_DIR)/$@ cmd/main.go
cd containerruntimediscovery/server && go build $(BUILD_OPTS) -ldflags="$(LDFLAGS)" -o $(ROOT_DIR)/$@ cmd/main.go

.PHONY: clean
clean: clean-ui clean-go ## Clean all build artifacts
Expand Down Expand Up @@ -215,6 +221,7 @@ BAKE_ENV += DOCKER_TAG=$(DOCKER_TAG)
BAKE_ENV += VERSION=$(VERSION)
BAKE_ENV += BUILD_TIMESTAMP=$(BUILD_TIMESTAMP)
BAKE_ENV += COMMIT_HASH=$(COMMIT_HASH)
BAKE_ENV += BUILD_OPTS="$(BUILD_OPTS)"

BAKE_OPTS =
ifneq ($(strip $(VMCLARITY_TOOLS_BASE)),)
Expand Down
2 changes: 2 additions & 0 deletions docker-bake.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ variable "VERSION" {default = null}
variable "BUILD_TIMESTAMP" {default = null}
variable "COMMIT_HASH" {default = null}
variable "VMCLARITY_TOOLS_BASE" {default = null}
variable "BUILD_OPTS" {default = null}

function "get_tag" {
params = [name]
Expand Down Expand Up @@ -40,6 +41,7 @@ target "_common_args_for_go" {
VERSION = "${VERSION}"
BUILD_TIMESTAMP = "${BUILD_TIMESTAMP}"
COMMIT_HASH = "${COMMIT_HASH}"
BUILD_OPTS = "${BUILD_OPTS}"
}
}

Expand Down
28 changes: 0 additions & 28 deletions orchestrator/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/Portshift/go-utils/healthz"

apiclient "github.com/openclarity/vmclarity/api/client"
apitypes "github.com/openclarity/vmclarity/api/types"
"github.com/openclarity/vmclarity/core/log"
discovery "github.com/openclarity/vmclarity/orchestrator/discoverer"
assetscanprocessor "github.com/openclarity/vmclarity/orchestrator/processor/assetscan"
Expand All @@ -33,12 +32,6 @@ import (
scanconfigwatcher "github.com/openclarity/vmclarity/orchestrator/watcher/scanconfig"
scanestimationwatcher "github.com/openclarity/vmclarity/orchestrator/watcher/scanestimation"
"github.com/openclarity/vmclarity/provider"
"github.com/openclarity/vmclarity/provider/aws"
"github.com/openclarity/vmclarity/provider/azure"
"github.com/openclarity/vmclarity/provider/docker"
"github.com/openclarity/vmclarity/provider/external"
"github.com/openclarity/vmclarity/provider/gcp"
"github.com/openclarity/vmclarity/provider/kubernetes"
)

type Orchestrator struct {
Expand Down Expand Up @@ -126,24 +119,3 @@ func (o *Orchestrator) Stop(ctx context.Context) {
o.cancelFunc()
}
}

// nolint:wrapcheck
// NewProvider returns an initialized provider.Provider based on the kind apitypes.CloudProvider.
func NewProvider(ctx context.Context, kind apitypes.CloudProvider) (provider.Provider, error) {
switch kind {
case apitypes.Azure:
return azure.New(ctx)
case apitypes.Docker:
return docker.New(ctx)
case apitypes.AWS:
return aws.New(ctx)
case apitypes.GCP:
return gcp.New(ctx)
case apitypes.External:
return external.New(ctx)
case apitypes.Kubernetes:
return kubernetes.New(ctx)
default:
return nil, fmt.Errorf("unsupported provider: %s", kind)
}
}
53 changes: 53 additions & 0 deletions orchestrator/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright © 2023 Cisco Systems, Inc. and its affiliates.
// All rights reserved.
//
// 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.

//go:build !providerv2

package orchestrator

import (
"context"
"fmt"

apitypes "github.com/openclarity/vmclarity/api/types"
"github.com/openclarity/vmclarity/provider"
"github.com/openclarity/vmclarity/provider/aws"
"github.com/openclarity/vmclarity/provider/azure"
"github.com/openclarity/vmclarity/provider/docker"
"github.com/openclarity/vmclarity/provider/external"
"github.com/openclarity/vmclarity/provider/gcp"
"github.com/openclarity/vmclarity/provider/kubernetes"
)

// nolint:wrapcheck
// NewProvider returns an initialized provider.Provider based on the kind apitypes.CloudProvider.
func NewProvider(ctx context.Context, kind apitypes.CloudProvider) (provider.Provider, error) {
switch kind {
case apitypes.Azure:
return azure.New(ctx)
case apitypes.Docker:
return docker.New(ctx)
case apitypes.AWS:
return aws.New(ctx)
case apitypes.GCP:
return gcp.New(ctx)
case apitypes.External:
return external.New(ctx)
case apitypes.Kubernetes:
return kubernetes.New(ctx)
default:
return nil, fmt.Errorf("unsupported provider: %s", kind)
}
}
53 changes: 53 additions & 0 deletions orchestrator/providerv2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright © 2023 Cisco Systems, Inc. and its affiliates.
// All rights reserved.
//
// 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.

//go:build providerv2

package orchestrator

import (
"context"
"fmt"

apitypes "github.com/openclarity/vmclarity/api/types"
provider2 "github.com/openclarity/vmclarity/provider"
"github.com/openclarity/vmclarity/provider/docker"
"github.com/openclarity/vmclarity/provider/external"
"github.com/openclarity/vmclarity/provider/kubernetes"
"github.com/openclarity/vmclarity/provider/v2/aws"
"github.com/openclarity/vmclarity/provider/v2/azure"
"github.com/openclarity/vmclarity/provider/v2/gcp"
)

// nolint:wrapcheck
// NewProvider returns an initialized provider.Provider based on the kind apitypes.CloudProvider.
func NewProvider(ctx context.Context, kind apitypes.CloudProvider) (provider2.Provider, error) {
switch kind {
case apitypes.Azure:
return azure.New(ctx)
case apitypes.Docker:
return docker.New(ctx)
case apitypes.AWS:
return aws.New(ctx)
case apitypes.GCP:
return gcp.New(ctx)
case apitypes.External:
return external.New(ctx)
case apitypes.Kubernetes:
return kubernetes.New(ctx)
default:
return nil, fmt.Errorf("unsupported provider: %s", kind)
}
}

0 comments on commit dae52dc

Please sign in to comment.