# Copyright 2021 The Kubeflow Authors
#
# 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.

FROM golang:1.17.6-alpine3.15 as builder

RUN apk update && apk upgrade
RUN apk add --no-cache git gcc musl-dev

WORKDIR /src/github.com/kubeflow/pipelines
COPY . .

RUN GO111MODULE=on go build -o /bin/controller backend/src/crd/controller/viewer/*.go
# Check licenses and comply with license terms.
RUN ./hack/install-go-licenses.sh
# First, make sure there's no forbidden license.
RUN go-licenses check ./backend/src/crd/controller/viewer
RUN go-licenses csv ./backend/src/crd/controller/viewer > /tmp/licenses.csv && \
    diff /tmp/licenses.csv backend/third_party_licenses/viewer.csv && \
    go-licenses save ./backend/src/crd/controller/viewer --save_path /tmp/NOTICES

FROM alpine
WORKDIR /bin

COPY --from=builder /bin/controller /bin/controller
RUN chmod +x /bin/controller

# Copy licenses and notices.
COPY --from=builder /tmp/licenses.csv /third_party/licenses.csv
COPY --from=builder /tmp/NOTICES /third_party/NOTICES

ENV MAX_NUM_VIEWERS "50"
ENV NAMESPACE "kubeflow"

CMD /bin/controller -logtostderr=true -max_num_viewers=${MAX_NUM_VIEWERS} --namespace=${NAMESPACE}
