Skip to content

Commit

Permalink
feat: add downloading progress for app image pull
Browse files Browse the repository at this point in the history
  • Loading branch information
hysyeah committed May 29, 2024
1 parent e7efc5f commit f6cf59d
Show file tree
Hide file tree
Showing 41 changed files with 2,211 additions and 129 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish_docker.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Publish to Dockerhub
name: Publish app-service to Dockerhub

on:
workflow_dispatch:
Expand Down
28 changes: 28 additions & 0 deletions .github/workflows/publish_imageservice.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Publish image-service to Dockerhub

on:
workflow_dispatch:
inputs:
tags:
description: 'Release Tags'

jobs:
update_dockerhub:
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v3

- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASS }}

- name: Build and push Docker image
uses: docker/build-push-action@v3
with:
push: true
tags: beclab/image-service:${{ github.event.inputs.tags }}
file: Dockerfile.image

29 changes: 29 additions & 0 deletions Dockerfile.image
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Build the manager binary
FROM golang:1.18 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod bytetrade.io/web3os/app-service/go.mod
COPY go.sum bytetrade.io/web3os/app-service/go.sum

RUN cd bytetrade.io/web3os/app-service && \
go mod download

# Copy the go source
COPY cmd/ bytetrade.io/web3os/app-service/cmd/
COPY api/ bytetrade.io/web3os/app-service/api/
COPY controllers/ bytetrade.io/web3os/app-service/controllers/
COPY pkg/ bytetrade.io/web3os/app-service/pkg/

# Build
RUN cd bytetrade.io/web3os/app-service && \
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -a -o image-service cmd/image-service/main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM gcr.io/distroless/static:debug
WORKDIR /
COPY --from=builder /workspace/bytetrade.io/web3os/app-service/image-service .

ENTRYPOINT ["/image-service"]
USER 65532:65532
3 changes: 2 additions & 1 deletion api/app.bytetrade.io/v1alpha1/application_states.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const (
// AppUpgrading means that an upgrade operation is underway.
AppUpgrading ApplicationState = "upgrading"
// AppResuming means that a resume operation is underway.
AppResuming ApplicationState = "resuming"
AppResuming ApplicationState = "resuming"
AppDownloading ApplicationState = "downloading"
)

func (a ApplicationState) String() string {
Expand Down
2 changes: 2 additions & 0 deletions api/app.bytetrade.io/v1alpha1/appmanager_states.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const (
// Stopping means that the suspend operation is underway.
Stopping ApplicationManagerState = "stopping"

Downloading ApplicationManagerState = "downloading"

// Processing means that the intermediate state of an operation, include
// installing,upgrading,uninstalling,resuming,canceling,stopping.
Processing ApplicationManagerState = "processing"
Expand Down
8 changes: 8 additions & 0 deletions api/app.bytetrade.io/v1alpha1/appmanager_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ type OpRecord struct {
StatusTime *metav1.Time `json:"statusTime"`
}

//type ImageProgress struct {
// AppName string `json:"appName"`
// OwnerName string `json:"ownerName"`
// NodeName string `json:"nodeName"`
// ImageRef string `json:"imageRef"`
// Progress string `json:"progress"`
//}

//+kubebuilder:object:root=true
//+k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

Expand Down
64 changes: 64 additions & 0 deletions api/app.bytetrade.io/v1alpha1/imagemanager_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

//+genclient
//+genclient:nonNamespaced
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:resource:scope=Cluster, shortName={im}, categories={all}
//+kubebuilder:printcolumn:JSONPath=.spec.appName, name=application name, type=string
//+kubebuilder:printcolumn:JSONPath=.spec.appNamespace, name=namespace, type=string
//+kubebuilder:printcolumn:JSONPath=.status.state, name=state, type=string
//+kubebuilder:printcolumn:JSONPath=.metadata.creationTimestamp, name=age, type=date
//+k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// ImageManager is the Schema for the image managers API
type ImageManager struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ImageManagerSpec `json:"spec,omitempty"`
Status ImageManagerStatus `json:"status,omitempty"`
}

// ImageManagerStatus defines the observed state of ApplicationManager
type ImageManagerStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
Conditions []ImageProgress `json:"conditions,omitempty"`
Message string `json:"message,omitempty"`
State string `json:"state"`
UpdateTime *metav1.Time `json:"updateTime"`
StatusTime *metav1.Time `json:"statusTime"`
}

// ImageManagerSpec defines the desired state of ImageManager
type ImageManagerSpec struct {
AppName string `json:"appName"`
AppNamespace string `json:"appNamespace,omitempty"`
AppOwner string `json:"appOwner,omitempty"`
Refs []string `json:"refs"`
}

type ImageProgress struct {
NodeName string `json:"nodeName"`
ImageRef string `json:"imageRef"`
Progress string `json:"progress"`
}

//+kubebuilder:object:root=true
//+k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// ImageManagerList contains a list of ApplicationManager
type ImageManagerList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []ImageManager `json:"items"`
}

func init() {
SchemeBuilder.Register(&ImageManager{}, &ImageManagerList{})
}
125 changes: 125 additions & 0 deletions api/app.bytetrade.io/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 88 additions & 0 deletions cmd/image-service/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package main

import (
appv1alpha1 "bytetrade.io/web3os/app-service/api/app.bytetrade.io/v1alpha1"
sysv1alpha1 "bytetrade.io/web3os/app-service/api/sys.bytetrade.io/v1alpha1"
"bytetrade.io/web3os/app-service/controllers"
"context"
"go.uber.org/zap/zapcore"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"os"
"os/signal"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"syscall"
)

var (
scheme = runtime.NewScheme()
imageLog = ctrl.Log.WithName("image")
)

func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(appv1alpha1.AddToScheme(scheme))
utilruntime.Must(sysv1alpha1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}

var shutdownSignals = []os.Signal{os.Interrupt, syscall.SIGTERM}

func main() {
opts := zap.Options{
Development: true,
TimeEncoder: zapcore.RFC3339TimeEncoder,
}
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
config := ctrl.GetConfigOrDie()
mgr, err := ctrl.NewManager(config, ctrl.Options{
Scheme: scheme,
MetricsBindAddress: ":8087",
HealthProbeBindAddress: ":7081",
LeaderElection: false,
})
if err != nil {
imageLog.Error(err, "Unable to start image manager")
os.Exit(1)
}

if err = (&controllers.ImageManagerController{
Client: mgr.GetClient(),
}).SetupWithManager(mgr); err != nil {
imageLog.Error(err, "Unable to create image controller")
os.Exit(1)
}

if err = mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
imageLog.Error(err, "Unable to set up health check")
os.Exit(1)
}
if err = mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
imageLog.Error(err, "Unable to set up ready check")
os.Exit(1)
}

ictx, cancelFunc := context.WithCancel(context.Background())

c := make(chan os.Signal, 2)
signal.Notify(c, shutdownSignals...)
go func() {
select {
case <-c:
cancelFunc()
<-c
os.Exit(1) // second signal. Exit directly.

}
}()

if err = mgr.Start(ictx); err != nil {
cancelFunc()
imageLog.Error(err, "Unable to running image manager")
os.Exit(1)
}
cancelFunc()
}
Loading

0 comments on commit f6cf59d

Please sign in to comment.