Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add interfaces for Image Watcher #902

Merged
merged 4 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["registry.go"],
importpath = "github.com/pipe-cd/pipe/pkg/app/piped/containerregistry",
srcs = ["gcs.go"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: gcr?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, a big mistake.

importpath = "github.com/pipe-cd/pipe/pkg/app/piped/imageprovider/gcs",
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package containerregistry watches the configured container registry
// for the new images.
package containerregistry
package gcs
9 changes: 9 additions & 0 deletions pkg/app/piped/imagewatcher/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["watcher.go"],
importpath = "github.com/pipe-cd/pipe/pkg/app/piped/imagewatcher",
visibility = ["//visibility:public"],
deps = ["@org_uber_go_zap//:go_default_library"],
)
79 changes: 79 additions & 0 deletions pkg/app/piped/imagewatcher/watcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2020 The PipeCD 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.

// Package imagewatcher provides a piped component
// that periodically checks the image registry and updates
// the image if there are differences with Git.
package imagewatcher

import (
"context"
"time"

"go.uber.org/zap"
)

type Watcher interface {
Run(context.Context) error
}

type watcher struct {
timer *time.Timer
logger *zap.Logger
}

type imageRepos map[string]imageRepo
type imageRepo struct {
}

func NewWatcher(interval time.Duration, logger *zap.Logger) Watcher {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logger is unused in NewWatcher

return &watcher{
timer: time.NewTimer(interval),
}
}

func (w *watcher) Run(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return nil
case <-w.timer.C:
reposInReg := w.fetchFromRegistry()
reposInGit := w.fetchFromGit()
outdated := calculateChanges(reposInReg, reposInGit)
if err := w.update(outdated); err != nil {
w.logger.Error("failed to update image", zap.Error(err))
}
}
}
return nil
}

func (w *watcher) fetchFromRegistry() imageRepos {
return nil
}

func (w *watcher) fetchFromGit() imageRepos {
return nil
}

func (w *watcher) update(targets imageRepos) error {
nakabonne marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

// calculateChanges compares between image repos in the image registry and
// image repos in git. And then gives back image repos to be updated.
func calculateChanges(x, y imageRepos) imageRepos {
nakabonne marked this conversation as resolved.
Show resolved Hide resolved
nakabonne marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
1 change: 0 additions & 1 deletion pkg/model/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ go_test(
data = glob(["testdata/**"]),
embed = [":go_default_library"],
deps = [
"//pkg/crypto:go_default_library",
"@com_github_stretchr_testify//assert:go_default_library",
"@org_golang_x_crypto//bcrypt:go_default_library",
],
Expand Down