Skip to content

Commit

Permalink
changelog: add proxy in the operator http server
Browse files Browse the repository at this point in the history
this will allow to display the changelog using the Rancher UI URL:

https://<RancherURL>/elemental/changelog/<ManagedOSVersion.Name>

which is set in the ManagedOSChangelog.status.changelogURL

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
  • Loading branch information
fgiudici committed Dec 9, 2024
1 parent 07c8bd4 commit f970d67
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 1 deletion.
109 changes: 109 additions & 0 deletions pkg/server/api_changelog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
Copyright © 2022 - 2024 SUSE LLC
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 server

import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"path"
"strings"

corev1 "k8s.io/api/core/v1"

elementalv1 "github.com/rancher/elemental-operator/api/v1beta1"
"k8s.io/apimachinery/pkg/types"
)

func (i *InventoryServer) apiChangelog(resp http.ResponseWriter, req *http.Request, splittedPath []string) error {
var err error
var changelog *elementalv1.ManagedOSChangelog

// expected splittedPath = {"changelog", {ManagedOSVersion.Name}}
if len(splittedPath) < 2 {
err = fmt.Errorf("unexpected path: %v", splittedPath)
http.Error(resp, err.Error(), http.StatusNotFound)
return err
}
osVersion := splittedPath[1]
filename := ""
if len(splittedPath) == 3 {
filename = splittedPath[2]
}

if changelog, err = i.getManagedOSChangelog(osVersion); err != nil {
http.Error(resp, err.Error(), http.StatusNotFound)
return err
}

svc := &corev1.Service{}
if err = i.Get(i, types.NamespacedName{Namespace: changelog.Namespace, Name: changelog.Name}, svc); err != nil {
errMsg := fmt.Errorf("failed to get service for changelog %s/%s: %w", changelog.Namespace, changelog.Name, err)
http.Error(resp, errMsg.Error(), http.StatusInternalServerError)
return errMsg
}

rawURL := fmt.Sprintf("http://%s/%s", svc.Spec.ClusterIP, filename)
seedImgURL, err := url.Parse(rawURL)
if err != nil {
errMsg := fmt.Errorf("failed to parse url '%s'", rawURL)
http.Error(resp, errMsg.Error(), http.StatusInternalServerError)
return errMsg
}
director := func(r *http.Request) {
r.URL = seedImgURL
}

reverseProxy := &httputil.ReverseProxy{
Director: director,
}
reverseProxy.ServeHTTP(resp, req)

return nil
}

func (i *InventoryServer) getManagedOSChangelog(osVersion string) (*elementalv1.ManagedOSChangelog, error) {
escapedToken := strings.Replace(osVersion, "\n", "", -1)
escapedToken = strings.Replace(escapedToken, "\r", "", -1)

changelogList := &elementalv1.ManagedOSChangelogList{}
if err := i.List(i, changelogList); err != nil {
return nil, fmt.Errorf("failed to list managedoschangelogs")
}

var changelog *elementalv1.ManagedOSChangelog

for _, c := range changelogList.Items {
if path.Base(c.Spec.ManagedOSVersion) == escapedToken {
changelog = (&c).DeepCopy()
break
}
}

if changelog == nil {
return nil, fmt.Errorf("failed to find changelog with ManagedOSVersion %s", escapedToken)
}

for _, condition := range changelog.Status.Conditions {
if condition.Status != "True" {

Check failure on line 103 in pkg/server/api_changelog.go

View workflow job for this annotation

GitHub Actions / lint

string `True` has 3 occurrences, make it a constant (goconst)
return nil, fmt.Errorf("changelog %s/%s is not ready", changelog.Namespace, changelog.Name)
}
}

return changelog, nil
}
6 changes: 5 additions & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ func (i *InventoryServer) ServeHTTP(resp http.ResponseWriter, req *http.Request)
log.Errorf("seedimage download: %s", err.Error())
return
}

case "changelog":
if err := i.apiChangelog(resp, req, splittedPath); err != nil {
log.Errorf("changelog download: %s", err.Error())
return
}
default:
log.Errorf("Unknown API: %s", api)
http.Error(resp, fmt.Sprintf("unknwon api: %s", api), http.StatusBadRequest)
Expand Down

0 comments on commit f970d67

Please sign in to comment.