Skip to content

Commit

Permalink
conditional status informers (#839)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgalsaleh committed Jul 23, 2020
1 parent d501051 commit 71403eb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion kotsadm/api/src/kots_app/kots_app.ts
Expand Up @@ -507,7 +507,7 @@ export class KotsApp {
return false;
}
const registryInfo = await stores.kotsAppStore.getAppRegistryDetails(this.id);
const rendered = await kotsRenderFile(this, stores, tmpl, registryInfo);
const rendered = await kotsRenderFile(this, this.currentSequence!, tmpl, registryInfo);
const backup = yaml.safeLoad(rendered);
const annotations = _.get(backup, "metadata.annotations") as any;
if (!_.isPlainObject(annotations)) {
Expand Down
4 changes: 2 additions & 2 deletions kotsadm/api/src/kots_app/kots_ffi.ts
Expand Up @@ -199,7 +199,7 @@ export async function kotsAppDownloadUpdateFromAirgap(airgapFile: string, app: K
}
}

export async function kotsRenderFile(app: KotsApp, stores: Stores, input: string, registryInfo: KotsAppRegistryDetails): Promise<string> {
export async function kotsRenderFile(app: KotsApp, sequence: number, input: string, registryInfo: KotsAppRegistryDetails): Promise<string> {
const filename = tmp.tmpNameSync();
fs.writeFileSync(filename, input);

Expand All @@ -208,7 +208,7 @@ export async function kotsRenderFile(app: KotsApp, stores: Stores, input: string
const archive = path.join(tmpDir.name, "archive.tar.gz");

try {
fs.writeFileSync(archive, await app.getArchive("" + (app.currentSequence!)));
fs.writeFileSync(archive, await app.getArchive("" + sequence));

const statusServer = new StatusServer();
await statusServer.start(tmpDir.name);
Expand Down
2 changes: 1 addition & 1 deletion kotsadm/api/src/snapshots/backup.ts
Expand Up @@ -36,7 +36,7 @@ export async function backup(stores: Stores, appId: string, scheduled: boolean):
}

const tmpl = await stores.snapshotsStore.getKotsBackupSpec(appId, deployedVersion.sequence);
const rendered = await kotsRenderFile(app, stores, tmpl, registryInfo);
const rendered = await kotsRenderFile(app, app.currentSequence!, tmpl, registryInfo);
const base = yaml.safeLoad(rendered) as Backup;
const spec = (base && base.spec) || {};

Expand Down
13 changes: 11 additions & 2 deletions kotsadm/api/src/sockets/kots/DeploySocket.ts
Expand Up @@ -3,7 +3,7 @@ import { IO, Nsp, SocketService, SocketSession, Socket } from "@tsed/socketio";
import { getPostgresPool } from "../../util/persistence/db";
import { KotsAppStore, UndeployStatus } from "../../kots_app/kots_app_store";
import { KotsAppStatusStore } from "../../kots_app/kots_app_status_store";
import { State, KotsApp } from "../../kots_app";
import { State, KotsApp, kotsRenderFile } from "../../kots_app";
import { Params } from "../../server/params";
import { ClusterStore, Cluster } from "../../cluster";
import { PreflightStore } from "../../preflight/preflight_store";
Expand Down Expand Up @@ -302,9 +302,18 @@ export class KotsDeploySocketService {
try {
const kotsAppSpec = await app.getKotsAppSpec(cluster.id, this.kotsAppStore)
if (kotsAppSpec && kotsAppSpec.statusInformers) {
// render status informers
const registryInfo = await this.kotsAppStore.getAppRegistryDetails(app.id);
const renderedInformers: string[] = [];
for (let i = 0; i < kotsAppSpec.statusInformers.length; i++) {
const informer = kotsAppSpec.statusInformers[i];
const rendered = await kotsRenderFile(app, deployedAppSequence, informer, registryInfo);
renderedInformers.push(rendered);
}
// send to kots operator
this.io.in(clusterSocketHistory.clusterId).emit("appInformers", {
app_id: app.id,
informers: kotsAppSpec.statusInformers,
informers: renderedInformers,
});
} else {
// no informers, set state to ready
Expand Down
17 changes: 10 additions & 7 deletions kotsadm/operator/pkg/client/client.go
Expand Up @@ -302,9 +302,7 @@ func (c *Client) registerHandlers(socketClient *socket.Client) error {

err = socketClient.On("appInformers", func(h *socket.Channel, args InformRequest) {
log.Printf("received an inform event: %#v", args)
if err := c.applyAppInformers(args.AppID, args.Informers); err != nil {
log.Printf("error running informer: %s", err.Error())
}
c.applyAppInformers(args.AppID, args.Informers)
})
if err != nil {
return errors.Wrap(err, "failed to add inform handler")
Expand Down Expand Up @@ -414,17 +412,22 @@ func runPreflight(preflightURI string, ignorePermissions bool) error {
return kubernetesApplier.Preflight(preflightURI, ignorePermissions)
}

func (c *Client) applyAppInformers(appID string, informerStrings []types.StatusInformerString) error {
func (c *Client) applyAppInformers(appID string, informerStrings []types.StatusInformerString) {
var informers []types.StatusInformer
for _, str := range informerStrings {
if str == "" {
continue
}
informer, err := str.Parse()
if err != nil {
return errors.Wrapf(err, "failed to parse informer %s", str)
log.Printf(fmt.Sprintf("failed to parse informer %s: %s", str, err.Error()))
continue // don't stop
}
informers = append(informers, informer)
}
c.appStateMonitor.Apply(appID, informers)
return nil
if len(informers) > 0 {
c.appStateMonitor.Apply(appID, informers)
}
}

func (c *Client) sendAppStatus(appStatus types.AppStatus) error {
Expand Down

0 comments on commit 71403eb

Please sign in to comment.