From a8d9f521f60a3771f69e5ba3610995814dc22720 Mon Sep 17 00:00:00 2001 From: asararatnakar Date: Sun, 9 Oct 2022 22:26:02 -0400 Subject: [PATCH 1/3] Pushing init image for k8s builder to work Signed-off-by: asararatnakar --- .github/workflows/init-image-build-n-push.yaml | 16 ++++++++++++++++ Makefile | 6 ++++++ sample-network/init/Dockerfile | 17 +++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 .github/workflows/init-image-build-n-push.yaml create mode 100644 sample-network/init/Dockerfile diff --git a/.github/workflows/init-image-build-n-push.yaml b/.github/workflows/init-image-build-n-push.yaml new file mode 100644 index 00000000..567151bc --- /dev/null +++ b/.github/workflows/init-image-build-n-push.yaml @@ -0,0 +1,16 @@ +name: Build Init image + +on: + push: + branches: [main] + +jobs: + image: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Build and Push Init Image + run: | + echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u $GITHUB_ACTOR --password-stdin + make init-image diff --git a/Makefile b/Makefile index b3f8e106..66adf94c 100644 --- a/Makefile +++ b/Makefile @@ -17,11 +17,13 @@ # IMAGE ?= ghcr.io/hyperledger-labs/fabric-operator +INIT_IMAGE ?= ghcr.io/hyperledger-labs/init TAG ?= $(shell git rev-parse --short HEAD) ARCH ?= $(shell go env GOARCH) OSS_GO_VER ?= 1.17.7 BUILD_DATE = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ") OS = $(shell go env GOOS) +K8S_TAG = v0.6.0 DOCKER_IMAGE_REPO ?= ghcr.io @@ -47,6 +49,10 @@ image: setup docker build --rm . -f Dockerfile $(BUILD_ARGS) -t $(IMAGE):$(TAG)-$(ARCH) docker tag $(IMAGE):$(TAG)-$(ARCH) $(IMAGE):latest-$(ARCH) +init-image: + docker build --rm . -f sample-network/init/Dockerfile --build-arg K8S_TAG=$(K8S_TAG) -t $(INIT_IMAGE):latest-$(ARCH) + docker push $(INIT_IMAGE):latest-$(ARCH) + govendor: @go mod vendor diff --git a/sample-network/init/Dockerfile b/sample-network/init/Dockerfile new file mode 100644 index 00000000..30de27e9 --- /dev/null +++ b/sample-network/init/Dockerfile @@ -0,0 +1,17 @@ +ARG K8S_TAG +FROM ghcr.io/hyperledgendary/k8s-fabric-peer:${K8S_TAG} as builder + +FROM registry.access.redhat.com/ubi8/ubi-minimal + +RUN microdnf update \ + && microdnf install -y \ + shadow-utils \ + iputils \ + tar \ + && groupadd -g 7051 ibp-user \ + && useradd -u 7051 -g ibp-user -s /bin/bash ibp-user \ + && microdnf remove shadow-utils \ + && microdnf clean all \ + && mkdir -p /opt/hyperledger/k8s_builder/bin; + +COPY --from=builder /opt/hyperledger/k8s_builder/bin/ /opt/hyperledger/k8s_builder/bin/ \ No newline at end of file From 00c4e70b89a4537e6b36b2ae1c31b56385bc035e Mon Sep 17 00:00:00 2001 From: asararatnakar Date: Mon, 10 Oct 2022 12:01:43 -0400 Subject: [PATCH 2/3] shared volume to copy k8s builder binraies from init to peer container Signed-off-by: asararatnakar --- .../workflows/init-image-build-n-push.yaml | 4 +-- Makefile | 12 ++++--- pkg/offering/base/peer/override/deployment.go | 17 ++++++++-- sample-network/README.md | 11 ------- sample-network/init/Dockerfile | 14 ++++++--- sample-network/network | 4 +-- scripts/init-build-push.sh | 31 +++++++++++++++++++ 7 files changed, 67 insertions(+), 26 deletions(-) create mode 100755 scripts/init-build-push.sh diff --git a/.github/workflows/init-image-build-n-push.yaml b/.github/workflows/init-image-build-n-push.yaml index 567151bc..3d3b6031 100644 --- a/.github/workflows/init-image-build-n-push.yaml +++ b/.github/workflows/init-image-build-n-push.yaml @@ -10,7 +10,7 @@ jobs: steps: - uses: actions/checkout@v2 - - name: Build and Push Init Image + - name: Build and Push Init Image if doesn't exists run: | echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u $GITHUB_ACTOR --password-stdin - make init-image + make init-image init-image-push diff --git a/Makefile b/Makefile index 66adf94c..b6af1540 100644 --- a/Makefile +++ b/Makefile @@ -17,13 +17,15 @@ # IMAGE ?= ghcr.io/hyperledger-labs/fabric-operator -INIT_IMAGE ?= ghcr.io/hyperledger-labs/init TAG ?= $(shell git rev-parse --short HEAD) ARCH ?= $(shell go env GOARCH) OSS_GO_VER ?= 1.17.7 BUILD_DATE = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ") OS = $(shell go env GOOS) -K8S_TAG = v0.6.0 + +INIT_IMAGE ?= ghcr.io/hyperledger-labs/init +K8S_BUILDER_TAG = v0.7.2 +INIT_TAG ?= 1.0.0 DOCKER_IMAGE_REPO ?= ghcr.io @@ -50,8 +52,10 @@ image: setup docker tag $(IMAGE):$(TAG)-$(ARCH) $(IMAGE):latest-$(ARCH) init-image: - docker build --rm . -f sample-network/init/Dockerfile --build-arg K8S_TAG=$(K8S_TAG) -t $(INIT_IMAGE):latest-$(ARCH) - docker push $(INIT_IMAGE):latest-$(ARCH) + INIT_IMAGE=$(INIT_IMAGE) INIT_TAG=$(INIT_TAG) ARCH=$(ARCH) K8S_BUILDER_TAG=$(K8S_BUILDER_TAG) scripts/init-build-push.sh "build" + +init-image-push: + INIT_IMAGE=$(INIT_IMAGE) INIT_TAG=$(INIT_TAG) ARCH=$(ARCH) scripts/init-build-push.sh scripts/init-build-push.sh "push" govendor: @go mod vendor diff --git a/pkg/offering/base/peer/override/deployment.go b/pkg/offering/base/peer/override/deployment.go index ab6233e9..622f2e0d 100644 --- a/pkg/offering/base/peer/override/deployment.go +++ b/pkg/offering/base/peer/override/deployment.go @@ -414,6 +414,9 @@ func (o *Override) V2Deployment(instance *current.IBPPeer, deployment *dep.Deplo } if instance.UsingCCLauncherImage() { dirs = append(dirs, "cclauncher/") + } else if isV24Peer(instance) { + dirs = append(dirs, "/opt/hyperledger/k8s_builder") + cmdFormat += ` && cp -r /opt/hyperledger/k8s_builder/bin /k8s_builder/` } var directories string @@ -454,6 +457,12 @@ func (o *Override) V2Deployment(instance *current.IBPPeer, deployment *dep.Deplo peerContainer.DeleteEnv("CORE_VM_DOCKER_ATTACHSTDOUT") deployment.AppendEmptyDirVolumeIfMissing(fmt.Sprintf("%s-cclauncher", instance.Name), corev1.StorageMediumMemory) + } else if isV24Peer(instance) { + // This is the case where it is a 2.4.x peer and using K8S Builder as external builder + volumeMountName := fmt.Sprintf("%s-k8sbuilder", instance.GetName()) + initContainer.AppendVolumeMountIfMissing(volumeMountName, "/k8s_builder") + peerContainer.AppendVolumeMountIfMissing(volumeMountName, "/opt/hyperledger/k8s_builder") + deployment.AppendEmptyDirVolumeIfMissing(fmt.Sprintf("%s-k8sbuilder", instance.Name), corev1.StorageMediumMemory) } // Append a k/v JSON substitution map to the peer env. @@ -635,8 +644,7 @@ func (o *Override) UpdateDeployment(instance *current.IBPPeer, k8sDep *appsv1.De if err != nil { return errors.Wrapf(err, "failed to update V2 fabric deployment for instance '%s'", instance.GetName()) } - peerVersion := version.String(instance.Spec.FabricVersion) - if peerVersion.EqualWithoutTag(version.V2_4_1) || peerVersion.GreaterThan(version.V2_4_1) { + if isV24Peer(instance) { err := o.V24DeploymentUpdate(instance, deployment) if err != nil { return errors.Wrapf(err, "failed to update V24 fabric deployment for instance '%s'", instance.GetName()) @@ -970,3 +978,8 @@ func hsmDaemonSettings(instance *current.IBPPeer, hsmConfig *config.HSMConfig, p deployment.AppendPVCVolumeIfMissing(pvcVolumeName, instance.PVCName()) } } + +func isV24Peer(instance *current.IBPPeer) bool { + peerVersion := version.String(instance.Spec.FabricVersion) + return peerVersion.EqualWithoutTag(version.V2_4_1) || peerVersion.GreaterThan(version.V2_4_1) +} diff --git a/sample-network/README.md b/sample-network/README.md index 5e5713c1..22f3fc72 100644 --- a/sample-network/README.md +++ b/sample-network/README.md @@ -132,17 +132,6 @@ The operator can also be configured for use with [fabric-builder-k8s](https://gi providing smooth and immediate _Chaincode Right Now!_ deployments. With the `k8s` builder, the peer node will directly manage the lifecycle of the chaincode pods. -Reconstruct the network with the "k8s-fabric-peer" image: -```shell -network down - -export TEST_NETWORK_PEER_IMAGE=ghcr.io/hyperledgendary/k8s-fabric-peer -export TEST_NETWORK_PEER_IMAGE_LABEL=v0.6.0 - -network up -network channel create -``` - Download a "k8s" chaincode package: ```shell curl -fsSL https://github.com/hyperledgendary/conga-nft-contract/releases/download/v0.1.1/conga-nft-contract-v0.1.1.tgz -o conga-nft-contract-v0.1.1.tgz diff --git a/sample-network/init/Dockerfile b/sample-network/init/Dockerfile index 30de27e9..6936a788 100644 --- a/sample-network/init/Dockerfile +++ b/sample-network/init/Dockerfile @@ -1,17 +1,21 @@ -ARG K8S_TAG -FROM ghcr.io/hyperledgendary/k8s-fabric-peer:${K8S_TAG} as builder FROM registry.access.redhat.com/ubi8/ubi-minimal +ARG K8S_BUILDER_TAG + RUN microdnf update \ && microdnf install -y \ shadow-utils \ iputils \ tar \ + wget \ && groupadd -g 7051 ibp-user \ && useradd -u 7051 -g ibp-user -s /bin/bash ibp-user \ && microdnf remove shadow-utils \ - && microdnf clean all \ - && mkdir -p /opt/hyperledger/k8s_builder/bin; + && microdnf clean all + +WORKDIR /opt/hyperledger/k8s_builder/bin -COPY --from=builder /opt/hyperledger/k8s_builder/bin/ /opt/hyperledger/k8s_builder/bin/ \ No newline at end of file +RUN wget https://github.com/hyperledger-labs/fabric-builder-k8s/releases/download/${K8S_BUILDER_TAG}/fabric-builder-k8s-${K8S_BUILDER_TAG}-Linux-X64.tgz \ + && tar -xzf fabric-builder-k8s-${K8S_BUILDER_TAG}-Linux-X64.tgz \ + && rm -rf fabric-builder-k8s-${K8S_BUILDER_TAG}-Linux-X64.tgz \ No newline at end of file diff --git a/sample-network/network b/sample-network/network index 51d962e6..83c16034 100755 --- a/sample-network/network +++ b/sample-network/network @@ -79,8 +79,8 @@ context TOOLS_IMAGE ${FABRIC_CONTAINER_REGISTRY}/fabric-tools context TOOLS_IMAGE_LABEL ${FABRIC_VERSION} context OPERATOR_IMAGE ghcr.io/hyperledger-labs/fabric-operator context OPERATOR_IMAGE_LABEL latest-amd64 -context INIT_IMAGE registry.access.redhat.com/ubi8/ubi-minimal -context INIT_IMAGE_LABEL latest +context INIT_IMAGE ghcr.io/hyperledger-labs/init +context INIT_IMAGE_LABEL 1.0.0-amd64 context GRPCWEB_IMAGE ghcr.io/hyperledger-labs/grpc-web context GRPCWEB_IMAGE_LABEL latest context COUCHDB_IMAGE couchdb diff --git a/scripts/init-build-push.sh b/scripts/init-build-push.sh new file mode 100755 index 00000000..62ab66c1 --- /dev/null +++ b/scripts/init-build-push.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# +# Copyright contributors to the Hyperledger Fabric Operator project +# +# SPDX-License-Identifier: Apache-2.0 +# +# 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. +# + +docker manifest inspect ${INIT_IMAGE}:${INIT_TAG}-${ARCH} >> /dev/null + +if [[ $? -ne 0 ]]; then + if [[ $1 = "build" ]]; then + docker build --rm . -f sample-network/init/Dockerfile --build-arg K8S_BUILDER_TAG=${K8S_BUILDER_TAG} -t ${INIT_IMAGE}:${INIT_TAG}-${ARCH} + docker tag ${INIT_IMAGE}:${INIT_TAG}-${ARCH} ${INIT_IMAGE}:latest-${ARCH} + elif [[ $1 = "push" ]]; then + docker push ${INIT_IMAGE}:latest-${ARCH} + docker push ${INIT_IMAGE}:${INIT_TAG}-${ARCH} + fi +fi From 46722ef346f99dd7ba21b3b56d174f9fdda24dae Mon Sep 17 00:00:00 2001 From: asararatnakar Date: Mon, 10 Oct 2022 13:50:19 -0400 Subject: [PATCH 3/3] Fix the tests as the init command has been updated now Signed-off-by: asararatnakar --- pkg/offering/base/peer/override/deployment_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/offering/base/peer/override/deployment_test.go b/pkg/offering/base/peer/override/deployment_test.go index b7762457..938e045e 100644 --- a/pkg/offering/base/peer/override/deployment_test.go +++ b/pkg/offering/base/peer/override/deployment_test.go @@ -860,7 +860,7 @@ var _ = Describe("Base Peer Deployment Overrides", func() { Expect(init.Command).To(HaveLen(3)) Expect(init.Command[0]).To(Equal("bash")) Expect(init.Command[1]).To(Equal("-c")) - Expect(init.Command[2]).To(Equal("DEFAULT_PERM=775 && DEFAULT_USER=7051 && DEFAULT_GROUP=1000 && PERM=$(stat -c \"%a\" /data/) && USER=$(stat -c \"%u\" /data/) && GROUP=$(stat -c \"%g\" /data/) && if [ ${PERM} != ${DEFAULT_PERM} ] || [ ${USER} != ${DEFAULT_USER} ] || [ ${GROUP} != ${DEFAULT_GROUP} ]; then chmod -R ${DEFAULT_PERM} /data/ && chown -R -H ${DEFAULT_USER}:${DEFAULT_GROUP} /data/; fi")) + Expect(init.Command[2]).To(Equal("DEFAULT_PERM=775 && DEFAULT_USER=7051 && DEFAULT_GROUP=1000 && PERM=$(stat -c \"%a\" /data/) && USER=$(stat -c \"%u\" /data/) && GROUP=$(stat -c \"%g\" /data/) && if [ ${PERM} != ${DEFAULT_PERM} ] || [ ${USER} != ${DEFAULT_USER} ] || [ ${GROUP} != ${DEFAULT_GROUP} ]; then chmod -R ${DEFAULT_PERM} /{data/,/opt/hyperledger/k8s_builder} && chown -R -H ${DEFAULT_USER}:${DEFAULT_GROUP} /{data/,/opt/hyperledger/k8s_builder}; fi && cp -r /opt/hyperledger/k8s_builder/bin /k8s_builder/")) }) }) }) @@ -911,7 +911,7 @@ var _ = Describe("Base Peer Deployment Overrides", func() { Expect(init.Command).To(HaveLen(3)) Expect(init.Command[0]).To(Equal("bash")) Expect(init.Command[1]).To(Equal("-c")) - Expect(init.Command[2]).To(Equal("DEFAULT_PERM=775 && DEFAULT_USER=7051 && DEFAULT_GROUP=1000 && PERM=$(stat -c \"%a\" /data/) && USER=$(stat -c \"%u\" /data/) && GROUP=$(stat -c \"%g\" /data/) && if [ ${PERM} != ${DEFAULT_PERM} ] || [ ${USER} != ${DEFAULT_USER} ] || [ ${GROUP} != ${DEFAULT_GROUP} ]; then chmod -R ${DEFAULT_PERM} /data/ && chown -R -H ${DEFAULT_USER}:${DEFAULT_GROUP} /data/; fi")) + Expect(init.Command[2]).To(Equal("DEFAULT_PERM=775 && DEFAULT_USER=7051 && DEFAULT_GROUP=1000 && PERM=$(stat -c \"%a\" /data/) && USER=$(stat -c \"%u\" /data/) && GROUP=$(stat -c \"%g\" /data/) && if [ ${PERM} != ${DEFAULT_PERM} ] || [ ${USER} != ${DEFAULT_USER} ] || [ ${GROUP} != ${DEFAULT_GROUP} ]; then chmod -R ${DEFAULT_PERM} /{data/,/opt/hyperledger/k8s_builder} && chown -R -H ${DEFAULT_USER}:${DEFAULT_GROUP} /{data/,/opt/hyperledger/k8s_builder}; fi && cp -r /opt/hyperledger/k8s_builder/bin /k8s_builder/")) }) }) }) @@ -938,7 +938,7 @@ var _ = Describe("Base Peer Deployment Overrides", func() { Expect(init.Command).To(HaveLen(3)) Expect(init.Command[0]).To(Equal("bash")) Expect(init.Command[1]).To(Equal("-c")) - Expect(init.Command[2]).To(Equal("DEFAULT_PERM=775 && DEFAULT_USER=7051 && DEFAULT_GROUP=1000 && PERM=$(stat -c \"%a\" /data/) && USER=$(stat -c \"%u\" /data/) && GROUP=$(stat -c \"%g\" /data/) && if [ ${PERM} != ${DEFAULT_PERM} ] || [ ${USER} != ${DEFAULT_USER} ] || [ ${GROUP} != ${DEFAULT_GROUP} ]; then chmod -R ${DEFAULT_PERM} /{data/,data/peer/ledgersData/stateLeveldb} && chown -R -H ${DEFAULT_USER}:${DEFAULT_GROUP} /{data/,data/peer/ledgersData/stateLeveldb}; fi")) + Expect(init.Command[2]).To(Equal("DEFAULT_PERM=775 && DEFAULT_USER=7051 && DEFAULT_GROUP=1000 && PERM=$(stat -c \"%a\" /data/) && USER=$(stat -c \"%u\" /data/) && GROUP=$(stat -c \"%g\" /data/) && if [ ${PERM} != ${DEFAULT_PERM} ] || [ ${USER} != ${DEFAULT_USER} ] || [ ${GROUP} != ${DEFAULT_GROUP} ]; then chmod -R ${DEFAULT_PERM} /{data/,data/peer/ledgersData/stateLeveldb,/opt/hyperledger/k8s_builder} && chown -R -H ${DEFAULT_USER}:${DEFAULT_GROUP} /{data/,data/peer/ledgersData/stateLeveldb,/opt/hyperledger/k8s_builder}; fi && cp -r /opt/hyperledger/k8s_builder/bin /k8s_builder/")) }) }) })