Set package initial status if its empty (#1522)

If the user applies package YAML file has no status field,
the package won't be able to be compiled or deployed due
to lack of status. This PR aims to add a check at buildermgr
to set initial package status to those packages.
This commit is contained in:
Ta-Ching Chen
2020-02-02 00:39:02 +08:00
committed by GitHub
parent e9804a5b76
commit dd07c68201
9 changed files with 204 additions and 14 deletions
+7
View File
@@ -268,6 +268,9 @@ type (
// PackageStatus contains the build status of a package also the build log for examination.
PackageStatus struct {
// TODO: Add another status field to indicate whether a package
// is ready for deploy instead of setting "none" in build status.
// BuildStatus is the package build status.
BuildStatus BuildStatus `json:"buildstatus,omitempty"`
@@ -684,3 +687,7 @@ type (
GetObjectMeta() metav1.Object
}
)
func (a Archive) IsEmpty() bool {
return len(a.Literal) == 0 && len(a.URL) == 0
}
+1 -1
View File
@@ -48,7 +48,7 @@ func Start(logger *zap.Logger, storageSvcUrl string, envBuilderNamespace string)
pkgWatcher := makePackageWatcher(bmLogger, fissionClient,
kubernetesClient, envBuilderNamespace, storageSvcUrl)
go pkgWatcher.watchPackages(fissionClient, kubernetesClient, envBuilderNamespace)
go pkgWatcher.watchPackages()
select {}
}
+61 -11
View File
@@ -75,12 +75,6 @@ func makePackageWatcher(logger *zap.Logger, fissionClient *crd.FissionClient, k8
// 6. Update package status to succeed state
// *. Update package status to failed state,if any one of steps above failed/time out
func (pkgw *packageWatcher) build(buildCache *cache.Cache, srcpkg *fv1.Package) {
// Ignore non-pending state packages.
if srcpkg.Status.BuildStatus != fv1.BuildStatusPending {
return
}
// Ignore duplicate build requests
key := fmt.Sprintf("%v-%v", srcpkg.ObjectMeta.Name, srcpkg.ObjectMeta.ResourceVersion)
_, err := buildCache.Set(key, srcpkg)
@@ -228,20 +222,76 @@ func (pkgw *packageWatcher) build(buildCache *cache.Cache, srcpkg *fv1.Package)
zap.String("package", fmt.Sprintf("%s.%s", pkg.ObjectMeta.Name, pkg.ObjectMeta.Namespace)))
}
func (pkgw *packageWatcher) watchPackages(fissionClient *crd.FissionClient,
kubernetesClient *kubernetes.Clientset, builderNamespace string) {
func (pkgw *packageWatcher) watchPackages() {
buildCache := cache.MakeCache(0, 0)
lw := k8sCache.NewListWatchFromClient(pkgw.fissionClient.CoreV1().RESTClient(), "packages", apiv1.NamespaceAll, fields.Everything())
pkgStore, controller := k8sCache.NewInformer(lw, &fv1.Package{}, 60*time.Second, k8sCache.ResourceEventHandlerFuncs{
processPkg := func(pkg *fv1.Package) {
var err error
if len(pkg.Status.BuildStatus) == 0 {
_, err = setInitialBuildStatus(pkgw.fissionClient, pkg)
if err != nil {
pkgw.logger.Error("error filling package status", zap.Error(err))
}
// once we update the package status, an update event
// will arrive and handle by UpdateFunc later. So we
// don't need to build the package at this moment.
return
}
// Only build pending state packages.
if pkg.Status.BuildStatus == fv1.BuildStatusPending {
go pkgw.build(buildCache, pkg)
}
}
pkgStore, controller := k8sCache.NewInformer(lw, &fv1.Package{}, 60*time.Minute, k8sCache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
pkg := obj.(*fv1.Package)
go pkgw.build(buildCache, pkg)
processPkg(pkg)
},
UpdateFunc: func(oldObj, newObj interface{}) {
oldPkg := oldObj.(*fv1.Package)
pkg := newObj.(*fv1.Package)
go pkgw.build(buildCache, pkg)
// TODO: Once enable "/status", check generation for spec changed instead.
// Before "/status" is enabled, the generation and resource version will be changed
// if we update the status of a package, hence we are not able to differentiate
// the spec change or status change. So we only build package which's status
// us "pending" and user have to use "kubectl replace" to update a package.
if oldPkg.ResourceVersion == pkg.ResourceVersion &&
pkg.Status.BuildStatus != fv1.BuildStatusPending {
return
}
processPkg(pkg)
},
})
pkgw.pkgStore = pkgStore
controller.Run(make(chan struct{}))
}
// setInitialBuildStatus sets initial build status to a package if it is empty.
// This normally occurs when the user applies package YAML files that have no status field
// through kubectl.
func setInitialBuildStatus(fissionClient *crd.FissionClient, pkg *fv1.Package) (*fv1.Package, error) {
pkg.Status = fv1.PackageStatus{
LastUpdateTimestamp: metav1.Time{Time: time.Now().UTC()},
}
if !pkg.Spec.Deployment.IsEmpty() {
// if the deployment archive is not empty,
// we assume it's a deployable package no matter
// the source archive is empty or not.
pkg.Status.BuildStatus = fv1.BuildStatusNone
} else if !pkg.Spec.Source.IsEmpty() {
pkg.Status.BuildStatus = fv1.BuildStatusPending
} else {
// mark package failed since we cannot do anything with it.
pkg.Status.BuildStatus = fv1.BuildStatusFailed
pkg.Status.BuildLog = "Both deploy and source archive are empty"
}
// TODO: use UpdateStatus to update status
return fissionClient.CoreV1().Packages(pkg.Namespace).Update(pkg)
}
-1
View File
@@ -18,7 +18,6 @@ package controller
import (
"context"
"go.uber.org/zap"
"github.com/fission/fission/pkg/crd"