Skip to content

Commit

Permalink
operator/register: drop unused code
Browse files Browse the repository at this point in the history
remove old code passing data via HTTP labels

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
  • Loading branch information
fgiudici committed Sep 1, 2022
1 parent d49adeb commit fa5f689
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 175 deletions.
44 changes: 0 additions & 44 deletions pkg/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,47 +134,3 @@ func sendData(conn *websocket.Conn, smbios bool, labels map[string]string) error
}
return nil
}

func addSMBIOSHeaders(header *http.Header) error {
data, err := dmidecode.Decode()
if err != nil {
return errors.Wrap(err, "failed to read dmidecode data")
}

var buf bytes.Buffer
b64Enc := base64.NewEncoder(base64.StdEncoding, &buf)

if err = json.NewEncoder(b64Enc).Encode(data); err != nil {
return errors.Wrap(err, "failed to encode dmidecode data")
}

_ = b64Enc.Close()

chunk := make([]byte, 875) //the chunk size
part := 1
for {
n, err := buf.Read(chunk)
if err != nil {
if err != io.EOF {
return errors.Wrap(err, "failed to read file in chunks")
}
break
}
header.Set(fmt.Sprintf("X-Cattle-Smbios-%d", part), string(chunk[0:n]))
part++
}
return nil
}

func addLabelsHeaders(header *http.Header, labels map[string]string) error {
var buf bytes.Buffer
b64Enc := base64.NewEncoder(base64.StdEncoding, &buf)

if err := json.NewEncoder(b64Enc).Encode(labels); err != nil {
return errors.Wrap(err, "failed to encode labels")
}

_ = b64Enc.Close()
header.Set("X-Cattle-Labels", buf.String())
return nil
}
71 changes: 0 additions & 71 deletions pkg/server/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package server

import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -354,73 +353,3 @@ func (i *InventoryServer) commitMachineInventory(inventory *elm.MachineInventory
}
return inventory, nil
}

func getSMBios(req *http.Request) (map[string]interface{}, error) {
var smbios string
// Old header sent by clients on commit < be788bcfd899977770d84c996abd967c30942822
headerOld := req.Header.Get("X-Cattle-Smbios")

// If old header not found try to get the new ones
if headerOld == "" {
// 200 * 875bytes per header = 175Kb of smbios data, should be enough?
for i := 1; i <= 200; i++ {
header := req.Header.Get(fmt.Sprintf("X-Cattle-Smbios-%d", i))
if header == "" {
break
}
smbios = smbios + header
}
} else {
smbios = headerOld
}

if smbios == "" {
logrus.Debug("No smbios headers")
return nil, nil
}

smbiosData, err := base64.StdEncoding.DecodeString(smbios)
if err != nil {
logrus.Error("Error decoding smbios string")
return nil, err
}
data := map[string]interface{}{}
return data, json.Unmarshal(smbiosData, &data)
}

func getLabels(req *http.Request) (map[string]string, error) {
in := req.Header.Get("X-Cattle-Labels")
if in == "" {
return nil, nil
}

labelString, err := base64.StdEncoding.DecodeString(in)
if err != nil {
return nil, err
}
var labels map[string]string
return labels, json.Unmarshal(labelString, &labels)
}

func fillLabels(req *http.Request, inventory *elm.MachineInventory) (bool, error) {
updated := false
labels, err := getLabels(req)

if err != nil {
return updated, fmt.Errorf("extracting labels from headers: %w", err)
}
if len(labels) == 0 {
return updated, nil
}

updated = true
logrus.Debugf("found labels in request headers: %#v", labels)
if inventory.Labels == nil {
inventory.Labels = labels
} else {
for k, v := range labels {
inventory.Labels[k] = v
}
}
return updated, nil
}
60 changes: 0 additions & 60 deletions pkg/server/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@ limitations under the License.
package server

import (
"bytes"
"encoding/base64"
"encoding/json"
"net/http"
"testing"

values "github.com/rancher/wrangler/pkg/data"
"gotest.tools/assert"
)

Expand Down Expand Up @@ -100,58 +95,3 @@ func TestBuildName(t *testing.T) {
assert.Equal(t, testCase.Output, buildName(data, testCase.Format))
}
}

func TestSmbios(t *testing.T) {
dmiEncoded := map[string]interface{}{}
values.PutValue(dmiEncoded, "Myself", "System Information", "Manufacturer")
var buf bytes.Buffer
b64Enc := base64.NewEncoder(base64.StdEncoding, &buf)
json.NewEncoder(b64Enc).Encode(dmiEncoded)
_ = b64Enc.Close()

testCase := []struct {
header http.Header
path []string // Path to the value
value string // Actual value
gotIt bool // Did we get the value
}{
{
http.Header{"X-Cattle-Smbios": {buf.String()}}, // Old header
[]string{"System Information", "Manufacturer"},
"Myself",
true,
},
{
http.Header{"X-Cattle-Smbios-1": {buf.String()}}, // New header
[]string{"System Information", "Manufacturer"},
"Myself",
true,
},
{
http.Header{"X-Cattle-Smbios-2": {buf.String()}}, // New header but missing the first part
[]string{"System Information", "Manufacturer"},
"",
false,
},
{
http.Header{}, // Empty header
[]string{"System Information", "Manufacturer"},
"",
false,
},
}

for _, test := range testCase {
data, err := getSMBios(&http.Request{Header: test.header})
assert.Equal(t, err, nil)
d, gotIt := values.GetValue(data, test.path...)
assert.Equal(t, gotIt, test.gotIt)
// Cant compare string and nil and values.GetValue returns either a string or a nil
if test.value == "" {
assert.Equal(t, d, nil)
} else {
assert.Equal(t, d, test.value)
}

}
}

0 comments on commit fa5f689

Please sign in to comment.