Skip to content

Commit

Permalink
Merge pull request kubev2v#168 from tupyy/proxy-store
Browse files Browse the repository at this point in the history
add proxy-url to agent's image
  • Loading branch information
tupyy authored Feb 25, 2025
2 parents d20c503 + 903a6f1 commit 910207c
Show file tree
Hide file tree
Showing 16 changed files with 424 additions and 48 deletions.
14 changes: 14 additions & 0 deletions api/v1alpha1/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -457,13 +457,27 @@ components:
- updatedAt
- onPremises

AgentProxy:
type: object
properties:
httpUrl:
type: string
httpsUrl:
type: string
noProxy:
type: string

SourceCreate:
type: object
properties:
name:
type: string
sshPublicKey:
type: string
proxy:
$ref: '#/components/schemas/AgentProxy'
certificateChain:
type: string
required:
- name

Expand Down
73 changes: 37 additions & 36 deletions api/v1alpha1/spec.gen.go

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

13 changes: 11 additions & 2 deletions api/v1alpha1/types.gen.go

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

9 changes: 9 additions & 0 deletions data/ignition.template
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ storage:
Volume=/home/core/.migration-planner:/agent:Z
Volume=/var/lib/data:/agent/persistent-data:Z
Environment=OPA_SERVER=127.0.0.1:8181
{{ if .HttpProxyUrl }}
Environment=HTTP_PROXY={{ .HttpProxyUrl }}
{{ end }}
{{ if .HttpsProxyUrl }}
Environment=HTTPS_PROXY={{ .HttpsProxyUrl }}
{{ end }}
{{ if .NoProxyDomain }}
Environment=NO_PROXY={{ .NoProxyDomain }}
{{ end }}
Network=host
UserNS=keep-id:uid=1001

Expand Down
15 changes: 14 additions & 1 deletion internal/cli/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type GenerateOptions struct {
AgentImageURL string
ServiceIP string
OutputImageFilePath string
HttpProxyUrl string
HttpsProxyUrl string
NoProxyDomain string
}

func DefaultGenerateOptions() *GenerateOptions {
Expand Down Expand Up @@ -90,6 +93,9 @@ func (o *GenerateOptions) Bind(fs *pflag.FlagSet) {
fs.StringVarP(&o.ImageType, "image-type", "t", "ova", "Type of the image. Only accepts ova and iso")
fs.StringVarP(&o.AgentImageURL, "agent-image-url", "u", "quay.io/kubev2v/migration-planner-agent:latest", "Quay url of the agent's image. Defaults to quay.io/kubev2v/migration-planner-agent:latest")
fs.StringVarP(&o.OutputImageFilePath, "output-file", "o", "", "Output image file path")
fs.StringVarP(&o.HttpProxyUrl, "http-proxy", "", "", "Url of HTTP_PROXY")
fs.StringVarP(&o.HttpsProxyUrl, "https-proxy", "", "", "Url of HTTPS_PROXY")
fs.StringVarP(&o.NoProxyDomain, "no-proxy", "", "", "list of domains without proxy")
}

func (o *GenerateOptions) Run(ctx context.Context, args []string) error {
Expand All @@ -109,7 +115,14 @@ func (o *GenerateOptions) Run(ctx context.Context, args []string) error {

source := *resp.JSON200

imageBuilder := image.NewImageBuilder(source.Id).WithPlannerAgentImage(o.AgentImageURL).WithPlannerService(o.ServiceIP)
imageBuilder := image.NewImageBuilder(source.Id).
WithPlannerAgentImage(o.AgentImageURL).
WithPlannerService(o.ServiceIP).
WithProxy(image.Proxy{
HttpUrl: o.HttpProxyUrl,
HttpsUrl: o.HttpsProxyUrl,
NoProxyDomain: o.NoProxyDomain,
})

switch o.ImageType {
case "iso":
Expand Down
21 changes: 21 additions & 0 deletions internal/image/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,17 @@ const (
defaultRHCOSImage = "rhcos-live.x86_64.iso"
)

type Proxy struct {
HttpUrl string
HttpsUrl string
NoProxyDomain string
}

type ImageBuilder struct {
SourceID string
SshKey string
Proxy Proxy
CertificateChain string
PlannerServiceUI string
PlannerService string
MigrationPlannerAgentImage string
Expand Down Expand Up @@ -149,6 +157,9 @@ func (b *ImageBuilder) generateIgnition() (string, error) {
InsecureRegistry: b.InsecureRegistry,
Token: b.Token,
PersistentDiskDevice: b.PersistentDiskDevice,
HttpProxyUrl: b.Proxy.HttpUrl,
HttpsProxyUrl: b.Proxy.HttpsUrl,
NoProxyDomain: b.Proxy.NoProxyDomain,
}

var buf bytes.Buffer
Expand Down Expand Up @@ -347,3 +358,13 @@ func (b *ImageBuilder) WithImageType(imageType ImageType) *ImageBuilder {
b.imageType = imageType
return b
}

func (b *ImageBuilder) WithProxy(proxy Proxy) *ImageBuilder {
b.Proxy = proxy
return b
}

func (b *ImageBuilder) WithCertificateChain(certs string) *ImageBuilder {
b.CertificateChain = certs
return b
}
3 changes: 3 additions & 0 deletions internal/image/ova.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ type IgnitionData struct {
Token string
PersistentDiskDevice string
SourceID string
HttpProxyUrl string
HttpsProxyUrl string
NoProxyDomain string
}

type Image interface {
Expand Down
24 changes: 24 additions & 0 deletions internal/service/mappers/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,30 @@ func SourceFromApi(id uuid.UUID, user auth.User, imageTokenKey string, resource
return source
}

func ImageInfraFromApi(sourceID uuid.UUID, resource *v1alpha1.CreateSourceJSONRequestBody) model.ImageInfra {
imageInfra := model.ImageInfra{
SourceID: sourceID,
}

if resource.Proxy != nil {
if resource.Proxy.HttpUrl != nil {
imageInfra.HttpProxyUrl = *resource.Proxy.HttpUrl
}
if resource.Proxy.HttpsUrl != nil {
imageInfra.HttpsProxyUrl = *resource.Proxy.HttpsUrl
}
if resource.Proxy.NoProxy != nil {
imageInfra.NoProxyDomains = *resource.Proxy.NoProxy
}
}

if resource.CertificateChain != nil {
imageInfra.CertificateChain = *resource.CertificateChain
}

return imageInfra
}

func UpdateSourceFromApi(m *model.Source, inventory api.Inventory) *model.Source {
m.Inventory = model.MakeJSONField(inventory)
m.VCenterID = inventory.Vcenter.Id
Expand Down
19 changes: 19 additions & 0 deletions internal/service/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ func (h *ServiceHandler) ListSources(ctx context.Context, request server.ListSou
func (h *ServiceHandler) CreateSource(ctx context.Context, request server.CreateSourceRequestObject) (server.CreateSourceResponseObject, error) {
user := auth.MustHaveUser(ctx)

ctx, err := h.store.NewTransactionContext(ctx)
if err != nil {
return server.CreateSource500JSONResponse{}, nil
}

// Generate a signing key for tokens for the source
// TODO: merge imageTokenKey and sshPublickKey with the rest of image infra
imageTokenKey, err := image.HMACKey(32)
if err != nil {
return server.CreateSource400JSONResponse{Message: err.Error()}, nil
Expand All @@ -69,6 +75,19 @@ func (h *ServiceHandler) CreateSource(ctx context.Context, request server.Create
return server.CreateSource400JSONResponse{Message: err.Error()}, nil
}

if request.Body.Proxy != nil || request.Body.CertificateChain != nil {
imageInfra := mappers.ImageInfraFromApi(source.ID, request.Body)
_, err := h.store.ImageInfra().Create(ctx, imageInfra)
if err != nil {
_, _ = store.Rollback(ctx)
return server.CreateSource500JSONResponse{}, nil
}
}

if _, err := store.Commit(ctx); err != nil {
return server.CreateSource500JSONResponse{}, nil
}

return server.CreateSource201JSONResponse(mappers.SourceToApi(*result)), nil
}

Expand Down
66 changes: 66 additions & 0 deletions internal/service/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,72 @@ var _ = Describe("source handler", Ordered, func() {
Expect(source.Name).To(Equal("test"))
})

It("successfully creates a source -- with proxy paramters defined", func() {
eventWriter := newTestWriter()

user := auth.User{
Username: "admin",
Organization: "admin",
}
ctx := auth.NewUserContext(context.TODO(), user)

toStrPtr := func(s string) *string {
return &s
}

srv := service.NewServiceHandler(s, events.NewEventProducer(eventWriter))
resp, err := srv.CreateSource(ctx, server.CreateSourceRequestObject{
Body: &v1alpha1.CreateSourceJSONRequestBody{
Name: "test",
Proxy: &v1alpha1.AgentProxy{
HttpUrl: toStrPtr("http"),
HttpsUrl: toStrPtr("https"),
NoProxy: toStrPtr("noproxy"),
},
},
})
Expect(err).To(BeNil())
source, ok := resp.(server.CreateSource201JSONResponse)
Expect(ok).To(BeTrue())
Expect(source.Name).To(Equal("test"))

count := 0
tx := gormdb.Raw("SELECT COUNT(*) FROM image_infras;").Scan(&count)
Expect(tx.Error).To(BeNil())
Expect(count).To(Equal(1))
})

It("successfully creates a source -- with certificate chain defined", func() {
eventWriter := newTestWriter()

user := auth.User{
Username: "admin",
Organization: "admin",
}
ctx := auth.NewUserContext(context.TODO(), user)

toStrPtr := func(s string) *string {
return &s
}

srv := service.NewServiceHandler(s, events.NewEventProducer(eventWriter))
resp, err := srv.CreateSource(ctx, server.CreateSourceRequestObject{
Body: &v1alpha1.CreateSourceJSONRequestBody{
Name: "test",
CertificateChain: toStrPtr("chain"),
},
})
Expect(err).To(BeNil())
source, ok := resp.(server.CreateSource201JSONResponse)
Expect(ok).To(BeTrue())
Expect(source.Name).To(Equal("test"))

count := 0
tx := gormdb.Raw("SELECT COUNT(*) FROM image_infras;").Scan(&count)
Expect(tx.Error).To(BeNil())
Expect(count).To(Equal(1))
})

AfterEach(func() {
gormdb.Exec("DELETE FROM agents;")
gormdb.Exec("DELETE FROM sources;")
Expand Down
Loading

0 comments on commit 910207c

Please sign in to comment.