Skip to content

sdk: don't import Kubernetes packages#2264

Merged
msanft merged 3 commits intomainfrom
msanft/sdk/size-optimization
Mar 18, 2026
Merged

sdk: don't import Kubernetes packages#2264
msanft merged 3 commits intomainfrom
msanft/sdk/size-optimization

Conversation

@msanft
Copy link
Member

@msanft msanft commented Mar 18, 2026

The SDK previously caused quite large code to be generated. The following test program results in a 50MB binary, for example:

package main

import (
	"context"
	"fmt"
	"net/http"
	"os"

	"github.com/edgelesssys/contrast/sdk"
	"github.com/spf13/afero"
)

func main() {
	nonce := make([]byte, 32)
	client := sdk.New().
		WithFSStore(afero.NewBasePathFs(afero.NewOsFs(), os.TempDir())).
		WithHTTPClient(http.DefaultClient)

	att, err := client.GetAttestation(context.Background(), "https://example.com", nonce)
	if err != nil {
		panic(err)
	}

	valid, err := client.ValidateAttestation(context.Background(), nonce, att)
	if err != nil {
		panic(err)
	}
	fmt.Println(valid)
}

When analyzing the code with go-size-analyzer, it becomes apparent that most of the size stems from Kubernetes Go packages:

image

Note that this doesn't even include contributions of those packages to .rodata and other sections for constants, strings, etc.

@thomasten managed to trace this back to the history package a while ago. This PR now addresses the bloat by splitting the subset of history that requires the Kubernetes imports, namely the ConfigMap-backed store, into a separate package. As Go links on package level, this is necessary to have it not be linked into the produced binaries. A separate commit then aligns the rest of the history package with those naming changes, extracting the other, Afero-backed store into a separate package. This change reduces the size of the test binary vom 50MB to 18MB, a reduction of almost 70%!

See the commit messages for the individual technical details.

@msanft msanft requested review from burgerdev and thomasten March 18, 2026 10:12
@msanft msanft added the no changelog PRs not listed in the release notes label Mar 18, 2026
@msanft msanft force-pushed the msanft/sdk/size-optimization branch 4 times, most recently from 6128858 to 5ead255 Compare March 18, 2026 10:38
Copy link
Member

@burgerdev burgerdev left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this!

msanft added 2 commits March 18, 2026 11:46
Previously, almost 70% of the SDK code size were
created by k8s.io packages that the SDK doesn't
need.
These were linked into the SDK from the history
package through the ConfigMap-backed store.
To address this, this commit splits the ConfigMap-
backed store with the k8s.io imports into a
separate configmapstore package.
To align with the configmapstore package, we also
split the AferoStore into a separate aferostore
package.
Then, since the package names now convey the
information about the backing store, the store
creation functions are renamed to just "New".
@msanft msanft force-pushed the msanft/sdk/size-optimization branch from 5ead255 to 8e60f8c Compare March 18, 2026 10:47
@msanft msanft requested a review from burgerdev March 18, 2026 10:47
Copy link
Member

@burgerdev burgerdev left a comment

Choose a reason for hiding this comment

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

lgtm

"strings"
"time"

"github.com/edgelesssys/contrast/internal/history"
Copy link
Member

Choose a reason for hiding this comment

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

There's still an inverse dependency here, but I can try to consolidate that later.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is from RecoverConfigMaps, which I'm not totally sure where to move to. Thanks!

@msanft msanft merged commit 7231115 into main Mar 18, 2026
14 checks passed
@msanft msanft deleted the msanft/sdk/size-optimization branch March 18, 2026 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no changelog PRs not listed in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants