Skip to content

Commit

Permalink
feat: add shipwright build runs to k8s-watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Panaetius committed Jan 8, 2025
1 parent a4904b4 commit 3752ca9
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
14 changes: 14 additions & 0 deletions k8s-watcher/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,17 @@ func NewAmaltheaSessionCacheFromConfig(ctx context.Context, config Config, names
res = &Cache{informer: informer, lister: lister, namespace: namespace, userIDLabel: config.UserIDLabel}
return
}

// NewShipwrightBuildRunCacheFromConfig generates a new server cache from a configuration and a specfic k8s namespace.
func NewShipwrightBuildRunCacheFromConfig(ctx context.Context, config Config, namespace string) (res *Cache, err error) {
k8sDynamicClient, err := initializeK8sDynamicClient()
if err != nil {
return
}
resource := schema.GroupVersionResource{Group: config.ShipwrightBuildRunGroup, Version: config.ShipwrightBuildRunVersion, Resource: config.ShipwrightBuildRunPlural}
factory := dynamicinformer.NewFilteredDynamicSharedInformerFactory(k8sDynamicClient, time.Minute, namespace, nil)
informer := factory.ForResource(resource).Informer()
lister := factory.ForResource(resource).Lister()
res = &Cache{informer: informer, lister: lister, namespace: namespace, userIDLabel: config.UserIDLabel}
return
}
15 changes: 15 additions & 0 deletions k8s-watcher/cache_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,18 @@ func NewAmaltheaSessionCacheCollectionFromConfigOrDie(ctx context.Context, confi
}
return &caches
}

// NewShipwrightBuildRunCacheCollectionFromConfigOrDie generates a new cache map from a configuration. If it cannot
// do this successfully it will terminate the program because the server cannot run at all if this
// step fails in any way and the program cannot recover from errors that occur here.
func NewShipwrightBuildRunCacheCollectionFromConfigOrDie(ctx context.Context, config Config) *CacheCollection {
caches := CacheCollection{}
for _, namespace := range config.Namespaces {
cache, err := NewShipwrightBuildRunCacheFromConfig(ctx, config, namespace)
if err != nil {
log.Fatalf("Cannot create cache collection: %v\n", err)
}
caches[namespace] = cache
}
return &caches
}
23 changes: 23 additions & 0 deletions k8s-watcher/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ type Config struct {
AmaltheaSessionVersion string
// The plural name of the AmaltheaSession resource that shoud be cached.
AmaltheaSessionPlural string
// The group of the ShipwrightBuildRun resource that shoud be cached.
ShipwrightBuildRunGroup string
// The version of the ShipwrightBuildRun resource that shoud be cached.
ShipwrightBuildRunVersion string
// The plural name of the ShipwrightBuildRun resource that shoud be cached.
ShipwrightBuildRunPlural string
// The port where the server will listen to for providing responses to requests
// about listing the cached resources or for returning specific resources.
Port int
Expand Down Expand Up @@ -90,6 +96,23 @@ func NewConfigFromEnvOrDie(prefix string) Config {
config.AmaltheaSessionPlural = "amaltheasessions"
}

if ibGroup, ok := os.LookupEnv(fmt.Sprintf("%sSHIPWRIGHT_BUILDRUN_GROUP", prefix)); ok {
config.ShipwrightBuildRunGroup = ibGroup
} else {
config.ShipwrightBuildRunGroup = "shipwright.io"
}

if ibVersion, ok := os.LookupEnv(fmt.Sprintf("%sSHIPWRIGHT_BUILDRUN_VERSION", prefix)); ok {
config.ShipwrightBuildRunVersion = ibVersion
} else {
config.ShipwrightBuildRunVersion = "v1beta1"
}

if ibPlural, ok := os.LookupEnv(fmt.Sprintf("%sSHIPWRIGHT_BUILDRUN_PLURAL", prefix)); ok {
config.ShipwrightBuildRunPlural = ibPlural
} else {
config.ShipwrightBuildRunPlural = "buildruns"
}
if port, ok := os.LookupEnv(fmt.Sprintf("%sPORT", prefix)); ok {
portInt, err := strconv.Atoi(port)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions k8s-watcher/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
type Server struct {
cachesJS CacheCollection
cachesAS CacheCollection
cachesIB CacheCollection
config Config
router *httprouter.Router
*http.Server
Expand Down Expand Up @@ -49,8 +50,10 @@ func (s *Server) Initialize(ctx context.Context) {
s.Handler = s
go s.cachesJS.run(ctx)
go s.cachesAS.run(ctx)
go s.cachesIB.run(ctx)
s.cachesJS.synchronize(ctx, s.config.CacheSyncTimeout)
s.cachesAS.synchronize(ctx, s.config.CacheSyncTimeout)
s.cachesIB.synchronize(ctx, s.config.CacheSyncTimeout)
}

func (s *Server) respond(w http.ResponseWriter, req *http.Request, data interface{}, err error) {
Expand All @@ -72,10 +75,12 @@ func (s *Server) respond(w http.ResponseWriter, req *http.Request, data interfac
func NewServerFromConfigOrDie(ctx context.Context, config Config) *Server {
cacheCollectionJS := NewJupyterServerCacheCollectionFromConfigOrDie(ctx, config)
cacheCollectionAS := NewAmaltheaSessionCacheCollectionFromConfigOrDie(ctx, config)
cacheCollectionIB := NewShipwrightBuildRunCacheCollectionFromConfigOrDie(ctx, config)
return &Server{
config: config,
cachesJS: *cacheCollectionJS,
cachesAS: *cacheCollectionAS,
cachesIB: *cacheCollectionIB,
router: httprouter.New(),
Server: &http.Server{
Addr: fmt.Sprintf(":%d", config.Port),
Expand Down
13 changes: 13 additions & 0 deletions k8s-watcher/server_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func (s *Server) registerRoutes() {
s.router.HandlerFunc("GET", "/sessions/:serverID", s.asGetOne)
s.router.HandlerFunc("GET", "/users/:userID/sessions", s.asUserID)
s.router.HandlerFunc("GET", "/users/:userID/sessions/:serverID", s.asUserIDServerID)
// Used for the shipwright operator in charge of image build custom resources
s.router.HandlerFunc("GET", "/buildruns", s.ibGetAll)
s.router.HandlerFunc("GET", "/buildruns/:buildID", s.ibGetOne)
}

func (s *Server) jsGetAll(w http.ResponseWriter, req *http.Request) {
Expand Down Expand Up @@ -67,6 +70,16 @@ func (s *Server) asUserIDServerID(w http.ResponseWriter, req *http.Request) {
s.respond(w, req, output, err)
}

func (s *Server) ibGetAll(w http.ResponseWriter, req *http.Request) {
output, err := s.cachesIB.getAll()
s.respond(w, req, output, err)
}

func (s *Server) ibGetOne(w http.ResponseWriter, req *http.Request) {
params := httprouter.ParamsFromContext(req.Context())
output, err := s.cachesIB.getByName(params.ByName("buildID"))
s.respond(w, req, output, err)
}
func (s *Server) handleHealthCheck(w http.ResponseWriter, req *http.Request) {
s.respond(w, req, map[string]string{"running": "ok"}, nil)
}

0 comments on commit 3752ca9

Please sign in to comment.