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

Commit

Permalink
feat: provider v2 (#1299)
Browse files Browse the repository at this point in the history
* feat: initial version of provider v2

* feat: add feature flag support to go builds

* fix: binary builds with CGO enabled
  • Loading branch information
chrisgacsal authored Feb 15, 2024
1 parent 2e98ed5 commit 493fc1f
Show file tree
Hide file tree
Showing 22 changed files with 589 additions and 42 deletions.
7 changes: 6 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 All @@ -41,6 +42,10 @@ RUN xx-verify /bin/vmclarity-apiserver

FROM alpine:3.19@sha256:c5b1261d6d3e43071626931fc004f70149baeba2c8ec672bd4f27761f8e1ad6b

RUN apk add --update --no-cache \
ca-certificates \
libc6-compat

COPY --from=builder ["/bin/vmclarity-apiserver", "/bin/vmclarity-apiserver"]

ENTRYPOINT ["/bin/vmclarity-apiserver"]
8 changes: 5 additions & 3 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 All @@ -44,7 +45,8 @@ RUN apk upgrade --quiet --no-cache

RUN apk add --update --no-cache \
util-linux \
ca-certificates
ca-certificates \
libc6-compat

COPY --from=builder /bin/vmclarity-cli /bin/vmclarity-cli

Expand Down
9 changes: 7 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 All @@ -43,4 +44,8 @@ FROM alpine:3.19@sha256:c5b1261d6d3e43071626931fc004f70149baeba2c8ec672bd4f27761

COPY --from=builder ["/bin/vmclarity-cr-discovery-server", "/bin/vmclarity-cr-discovery-server"]

RUN apk add --update --no-cache \
ca-certificates \
libc6-compat

ENTRYPOINT ["/bin/vmclarity-cr-discovery-server"]
7 changes: 6 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 All @@ -43,4 +44,8 @@ FROM alpine:3.19@sha256:c5b1261d6d3e43071626931fc004f70149baeba2c8ec672bd4f27761

COPY --from=builder ["/bin/vmclarity-orchestrator", "/bin/vmclarity-orchestrator"]

RUN apk add --update --no-cache \
ca-certificates \
libc6-compat

ENTRYPOINT ["/bin/vmclarity-orchestrator"]
9 changes: 7 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 All @@ -43,6 +44,10 @@ FROM alpine:3.19@sha256:c5b1261d6d3e43071626931fc004f70149baeba2c8ec672bd4f27761

COPY --from=builder ["/bin/vmclarity-ui-backend", "/bin/vmclarity-ui-backend"]

RUN apk add --update --no-cache \
ca-certificates \
libc6-compat

ENTRYPOINT ["/bin/vmclarity-ui-backend"]

CMD ["--help"]
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)
}
}
31 changes: 31 additions & 0 deletions provider/v2/aws/discoverer/discoverer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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.

package discoverer

import (
"context"

"github.com/openclarity/vmclarity/provider"
)

var _ provider.Discoverer = &Discoverer{}

type Discoverer struct{}

func (d *Discoverer) DiscoverAssets(ctx context.Context) provider.AssetDiscoverer {
// TODO implement me
panic("implement me")
}
Loading

0 comments on commit 493fc1f

Please sign in to comment.