Skip to content

Commit

Permalink
Added version management & Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rahul Varma committed Aug 11, 2021
1 parent 11d3c87 commit a7764b9
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/publish-images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
- name: Set env vars for the job
run: |
grep -v '\#' versions.txt | grep opentelemetry-collector | awk -F= '{print "OTELCOL_VERSION="$2}' >> $GITHUB_ENV
grep -v '\#' versions.txt | grep targetallocator | awk -F= '{print "TARGETALLOCATOR_VERSION="$2}' >> $GITHUB_ENV
echo "VERSION_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_ENV
echo "VERSION=$(git describe --tags | sed 's/^v//')" >> $GITHUB_ENV
Expand Down Expand Up @@ -72,5 +73,6 @@ jobs:
VERSION=${{ env.VERSION }}
VERSION_DATE=${{ env.VERSION_DATE }}
OTELCOL_VERSION=${{ env.OTELCOL_VERSION }}
TARGETALLOCATOR_VERSION=${{ env.TARGETALLOCATOR_VERSION }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ ARG VERSION_PKG
ARG VERSION
ARG VERSION_DATE
ARG OTELCOL_VERSION
ARG TARGETALLOCATOR_VERSION

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -ldflags="-X ${VERSION_PKG}.version=${VERSION} -X ${VERSION_PKG}.buildDate=${VERSION_DATE} -X ${VERSION_PKG}.otelCol=${OTELCOL_VERSION}" -a -o manager main.go
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -ldflags="-X ${VERSION_PKG}.version=${VERSION} -X ${VERSION_PKG}.buildDate=${VERSION_DATE} -X ${VERSION_PKG}.otelCol=${OTELCOL_VERSION} -X ${VERSION_PKG}.targetAllocator=${TARGETALLOCATOR_VERSION}" -a -o manager main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ VERSION_DATE ?= $(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
VERSION_PKG ?= "github.com/open-telemetry/opentelemetry-operator/internal/version"
OTELCOL_VERSION ?= "$(shell grep -v '\#' versions.txt | grep opentelemetry-collector | awk -F= '{print $$2}')"
OPERATOR_VERSION ?= "$(shell grep -v '\#' versions.txt | grep operator | awk -F= '{print $$2}')"
LD_FLAGS ?= "-X ${VERSION_PKG}.version=${VERSION} -X ${VERSION_PKG}.buildDate=${VERSION_DATE} -X ${VERSION_PKG}.otelCol=${OTELCOL_VERSION}"
TARGETALLOCATOR_VERSION ?= "$(shell grep -v '\#' versions.txt | grep targetallocator | awk -F= '{print $$2}')"
LD_FLAGS ?= "-X ${VERSION_PKG}.version=${VERSION} -X ${VERSION_PKG}.buildDate=${VERSION_DATE} -X ${VERSION_PKG}.otelCol=${OTELCOL_VERSION} -X ${VERSION_PKG}.targetAllocator=${TARGETALLOCATOR_VERSION}"

# Image URL to use all building/pushing image targets
IMG_PREFIX ?= quay.io/${USER}
Expand Down Expand Up @@ -122,7 +123,7 @@ set-test-image-vars:

# Build the container image, used only for local dev purposes
container:
docker build -t ${IMG} --build-arg VERSION_PKG=${VERSION_PKG} --build-arg VERSION=${VERSION} --build-arg VERSION_DATE=${VERSION_DATE} --build-arg OTELCOL_VERSION=${OTELCOL_VERSION} .
docker build -t ${IMG} --build-arg VERSION_PKG=${VERSION_PKG} --build-arg VERSION=${VERSION} --build-arg VERSION_DATE=${VERSION_DATE} --build-arg OTELCOL_VERSION=${OTELCOL_VERSION} --build-arg TARGETALLOCATOR_VERSION=${TARGETALLOCATOR_VERSION} .

# Push the container image, used only for local dev purposes
container-push:
Expand Down
4 changes: 2 additions & 2 deletions internal/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func New(opts ...Option) Config {
}

if len(o.targetAllocatorImage) == 0 {
// will be replcaed with target allocator image once approved and available
o.targetAllocatorImage = "raul9595/otel-loadbalancer:0.0.1"
// will be replaced with target allocator image once approved and available
o.targetAllocatorImage = fmt.Sprintf("raul9595/otel-loadbalancer:%s", o.version.TargetAllocator)
}

return Config{
Expand Down
23 changes: 19 additions & 4 deletions internal/version/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import (
)

var (
version string
buildDate string
otelCol string
version string
buildDate string
otelCol string
targetAllocator string
)

// Version holds this Operator's version as well as the version of some of the components it uses.
Expand All @@ -32,6 +33,7 @@ type Version struct {
BuildDate string `json:"build-date"`
OpenTelemetryCollector string `json:"opentelemetry-collector-version"`
Go string `json:"go-version"`
TargetAllocator string `json:"target-allocator-version"`
}

// Get returns the Version object with the relevant information.
Expand All @@ -41,16 +43,18 @@ func Get() Version {
BuildDate: buildDate,
OpenTelemetryCollector: OpenTelemetryCollector(),
Go: runtime.Version(),
TargetAllocator: TargetAllocator(),
}
}

func (v Version) String() string {
return fmt.Sprintf(
"Version(Operator='%v', BuildDate='%v', OpenTelemetryCollector='%v', Go='%v')",
"Version(Operator='%v', BuildDate='%v', OpenTelemetryCollector='%v', Go='%v', TargetAllocator='%v')",
v.Operator,
v.BuildDate,
v.OpenTelemetryCollector,
v.Go,
v.TargetAllocator,
)
}

Expand All @@ -64,3 +68,14 @@ func OpenTelemetryCollector() string {
// fallback value, useful for tests
return "0.0.0"
}

// TargetAllocator returns the default TargetAllocator to use when no versions are specified via CLI or configuration.
func TargetAllocator() string {
if len(targetAllocator) > 0 {
// this should always be set, as it's specified during the build
return targetAllocator
}

// fallback value, useful for tests
return "0.0.0"
}
15 changes: 15 additions & 0 deletions internal/version/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,18 @@ func TestVersionFromBuild(t *testing.T) {
assert.Equal(t, otelCol, OpenTelemetryCollector())
assert.Contains(t, Get().String(), otelCol)
}

func TestTargetAllocatorFallbackVersion(t *testing.T) {
assert.Equal(t, "0.0.0", TargetAllocator())
}

func TestTargetAllocatorVersionFromBuild(t *testing.T) {
// prepare
targetAllocator = "0.0.2" // set during the build
defer func() {
targetAllocator = ""
}()

assert.Equal(t, targetAllocator, TargetAllocator())
assert.Contains(t, Get().String(), targetAllocator)
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func main() {
logger.Info("Starting the OpenTelemetry Operator",
"opentelemetry-operator", v.Operator,
"opentelemetry-collector", v.OpenTelemetryCollector,
"opentelemetry-targetallocator", v.TargetAllocator,
"build-date", v.BuildDate,
"go-version", v.Go,
"go-arch", runtime.GOARCH,
Expand Down
4 changes: 2 additions & 2 deletions pkg/collector/reconcile/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func ConfigMaps(ctx context.Context, params Params) error {
}

if params.Instance.Spec.TargetAllocator.Enabled {
cm, err := desiredTAConfigMap(ctx, params)
cm, err := desiredTAConfigMap(params)
if err != nil {
return fmt.Errorf("failed to parse config: %v", err)
}
Expand Down Expand Up @@ -79,7 +79,7 @@ func desiredConfigMap(_ context.Context, params Params) corev1.ConfigMap {
}
}

func desiredTAConfigMap(_ context.Context, params Params) (corev1.ConfigMap, error) {
func desiredTAConfigMap(params Params) (corev1.ConfigMap, error) {
name := naming.TAConfigMap(params.Instance)
labels := targetallocator.Labels(params.Instance)
labels["app.kubernetes.io/name"] = name
Expand Down
8 changes: 4 additions & 4 deletions pkg/collector/reconcile/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ label_selector:
`,
}

actual, err := desiredTAConfigMap(context.Background(), params())
actual, err := desiredTAConfigMap(params())
assert.NoError(t, err)

assert.Equal(t, "test-targetallocator", actual.Name)
Expand All @@ -109,7 +109,7 @@ label_selector:

func TestExpectedConfigMap(t *testing.T) {
t.Run("should create collector and target allocator config maps", func(t *testing.T) {
configMap, err := desiredTAConfigMap(context.Background(), params())
configMap, err := desiredTAConfigMap(params())
assert.NoError(t, err)
err = expectedConfigMaps(context.Background(), params(), []v1.ConfigMap{desiredConfigMap(context.Background(), params()), configMap}, true)
assert.NoError(t, err)
Expand Down Expand Up @@ -194,11 +194,11 @@ func TestExpectedConfigMap(t *testing.T) {
Scheme: testScheme,
Log: logger,
}
cm, err := desiredTAConfigMap(context.Background(), param)
cm, err := desiredTAConfigMap(param)
assert.EqualError(t, err, "no receivers available as part of the configuration")
createObjectIfNotExists(t, "test-targetallocator", &cm)

configMap, err := desiredTAConfigMap(context.Background(), params())
configMap, err := desiredTAConfigMap(params())
assert.NoError(t, err)
err = expectedConfigMaps(context.Background(), params(), []v1.ConfigMap{configMap}, true)
assert.NoError(t, err)
Expand Down
16 changes: 10 additions & 6 deletions pkg/collector/reconcile/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ func TestExpectedDeployments(t *testing.T) {

t.Run("should not update target allocator deployment when the config is updated", func(t *testing.T) {
ctx := context.Background()
createObjectIfNotExists(t, "test-targetallocator", &expectedDeploy)
orgUID := expectedDeploy.OwnerReferences[0].UID
createObjectIfNotExists(t, "test-targetallocator", &expectedTADeploy)
orgUID := expectedTADeploy.OwnerReferences[0].UID

updatedParam, err := newParams("test/test-img")
assert.NoError(t, err)
Expand All @@ -133,15 +133,17 @@ func TestExpectedDeployments(t *testing.T) {

assert.NoError(t, err)
assert.True(t, exists)
t.Log(expectedTADeploy.Spec.Template.Spec.Containers[0].Image)
t.Log(actual.Spec.Template.Spec.Containers[0].Image)
assert.Equal(t, orgUID, actual.OwnerReferences[0].UID)
assert.Equal(t, expectedDeploy.Spec.Template.Spec.Containers[0], actual.Spec.Template.Spec.Containers[0])
assert.Equal(t, expectedTADeploy.Spec.Template.Spec.Containers[0].Image, actual.Spec.Template.Spec.Containers[0].Image)
assert.Equal(t, int32(1), *actual.Spec.Replicas)
})

t.Run("should update target allocator deployment when the container image is updated", func(t *testing.T) {
ctx := context.Background()
createObjectIfNotExists(t, "test-targetallocator", &expectedDeploy)
orgUID := expectedDeploy.OwnerReferences[0].UID
createObjectIfNotExists(t, "test-targetallocator", &expectedTADeploy)
orgUID := expectedTADeploy.OwnerReferences[0].UID

updatedParam, err := newParams("test/test-img")
assert.NoError(t, err)
Expand All @@ -156,7 +158,9 @@ func TestExpectedDeployments(t *testing.T) {
assert.NoError(t, err)
assert.True(t, exists)
assert.Equal(t, orgUID, actual.OwnerReferences[0].UID)
assert.NotEqual(t, expectedDeploy.Spec.Template.Spec.Containers[0], actual.Spec.Template.Spec.Containers[0])
t.Log(expectedTADeploy.Spec.Template.Spec.Containers[0].Image)
t.Log(actual.Spec.Template.Spec.Containers[0].Image)
assert.NotEqual(t, expectedTADeploy.Spec.Template.Spec.Containers[0].Image, actual.Spec.Template.Spec.Containers[0].Image)
assert.Equal(t, int32(1), *actual.Spec.Replicas)
})

Expand Down
4 changes: 2 additions & 2 deletions pkg/collector/reconcile/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ func desiredTAService(params Params) corev1.Service {
Selector: selector,
Ports: []corev1.ServicePort{{
Name: "targetallocation",
Port: 443,
TargetPort: intstr.FromInt(443),
Port: 80,
TargetPort: intstr.FromInt(8080),
}},
},
}
Expand Down
8 changes: 2 additions & 6 deletions pkg/collector/reconcile/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,14 @@ func params() Params {
}
}

func newParams(containerImage ...string) (Params, error) {
func newParams(containerImage string) (Params, error) {
replicas := int32(1)
configYAML, err := ioutil.ReadFile("test.yaml")
if err != nil {
return Params{}, fmt.Errorf("Error getting yaml file: %v", err)
}

cfg := config.New()
defaultContainerImage := cfg.TargetAllocatorImage()
if len(containerImage) > 0 && len(containerImage[0]) > 0 {
defaultContainerImage = containerImage[0]
}

return Params{
Config: cfg,
Expand Down Expand Up @@ -159,7 +155,7 @@ func newParams(containerImage ...string) (Params, error) {
}},
TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{
Enabled: true,
Image: defaultContainerImage,
Image: containerImage,
},
Replicas: &replicas,
Config: string(configYAML),
Expand Down
3 changes: 3 additions & 0 deletions versions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ opentelemetry-collector=0.31.0

# Represents the current release of the OpenTelemetry Operator.
operator=0.31.0

# Represents the current release of the Target Allocator.
targetallocator=0.1.0

0 comments on commit a7764b9

Please sign in to comment.