פריסת feature flags באמצעות Google Cloud המסוף
לפני שמתחילים
-
נכנסים לחשבון Google.
אם עדיין אין חשבון, יוצרים חשבון חדש.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the App Lifecycle Manager, SaaS Config, App Design Center, Artifact Registry, Cloud Storage, and Infrastructure Manager APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the App Lifecycle Manager, SaaS Config, App Design Center, Artifact Registry, Cloud Storage, and Infrastructure Manager APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
מוודאים שלחשבון המשתמש יש את התפקידים הבאים של ניהול זהויות והרשאות גישה בפרויקט ב- Google Cloud :
- אדמין של App Lifecycle Manager (
roles/saasservicemgmt.admin) - אדמין ב-App Design Center (
roles/designcenter.admin) - צפייה בהגדרות של SaaS (
roles/saasconfig.viewer) - אדמין בחשבון שירות (
roles/iam.serviceAccountAdmin) ואדמין IAM בפרויקט (roles/resourcemanager.projectIamAdmin)
- אדמין של App Lifecycle Manager (
-
מוודאים שמרחב העבודה של App Design Center וחשבונות השירות של App Lifecycle Manager מוגדרים, כולל חשבון שירות לסימולציה בניהול המשתמש, שקיבל את התפקידים "יצירת אסימונים בחשבון שירות" (
roles/iam.serviceAccountTokenCreator), "אדמין של Infra Manager" (roles/config.admin) ו"אדמין של Storage" (roles/storage.admin) בפרויקט. מידע נוסף על חשבונות שירות של App Lifecycle Manager זמין במאמר חשבונות שירות. -
מתקינים את Python בגרסה 3.10 ואילך ואת
pipבמכונה המקומית, ואז מאמתים את Google Cloud CLI בשביל Application Default Credentials (ADC):gcloud auth application-default login
יצירת מוצר SaaS במסוף
יוצרים משאב של הצעה ל-SaaS בתור מאגר ברמה העליונה למודל השירות ול-feature flags:
במסוף Google Cloud , נכנסים לדף App Lifecycle Manager > SaaS offerings.
לוחצים על יצירה.
בשדה SaaS offering name, מזינים
flags-quickstart-offering.ברשימה Region, בוחרים באפשרות
us-central1ולוחצים על OK.לוחצים על יצירה. בסיום היצירה, נפתח הדף SaaS offering details.
בניית מודל של השירות ב-App Design Center
בתהליך העבודה שמתמקד באפליקציות, מגדירים סוגי יחידות על ידי יצירת תבנית של אפליקציה ב-App Design Center. אם אתם רוצים להשתמש ב-feature flags בלי להקצות תשתית ענן דרך App Lifecycle Manager, אתם צריכים ליצור תבנית מורכבת שמכילה תבנית רגילה ריקה של אפליקציה לכל סוג של שירות בארכיטקטורה שלכם (לדוגמה, אפליקציית בקאנד, אפליקציית פרונטאנד או שירות שער). תבנית ריקה רגילה מצהירה על סוג שירות בלי לפרוס משאבי ענן. כשתהיו מוכנים לנהל את פריסות התשתית באמצעות App Lifecycle Manager, תוכלו להוסיף רכיבי Terraform לתבנית הרגילה.
כדי ליצור תבנית רגילה ריקה בתוך תבנית מורכבת וליצור סוג יחידה, מבצעים את השלבים הבאים:
- בדף SaaS offering details של
flags-quickstart-offering, לוחצים על Create new template. - בשדה Template ID, מזינים את הערך
flags-composite-templateולוחצים על Create template. - בקטע Connected application template, לוחצים על התפריט Manage template ואז על Configure in canvas כדי לפתוח את App Design Center.
- באזור Components בקנבס העיצוב, לוחצים על Embed template ואז על Create a new template.
- בשדה Template ID, מזינים
backend-service-templateולוחצים על Create template כדי לפתוח את אזור העריכה של התבנית הרגילה. - משאירים את אזור העריכה של
backend-service-templateריק בלי להוסיף רכיבים, ולוחצים על Add to Catalog כדי לפרסם את התבנית הרגילה בקטלוג של המרחב. - חוזרים לאזור העריכה של
flags-composite-template, לוחצים על Embed template, בוחרים אתbackend-service-templateמתוך Templates ולוחצים על Select. - לוחצים על Add to Catalog כדי לפרסם את גרסת התבנית המשולבת, ואז לוחצים על Continue ואז על Deploy at scale with App Lifecycle Manager.
- בקטע Connected application template בדף SaaS offering details, לוחצים על Update template revision, בוחרים את הגרסה האחרונה של
flags-composite-templateולוחצים על Update (או לוחצים על Connect existing template אם מחברים תבנית שנוצרה ישירות ב-App Design Center). - פותחים את הכרטיסייה Unit kinds בדף SaaS offering details ומוודאים ש-App Lifecycle Manager יצר את סוג היחידה של
backend-service-template.
יצירה והקצאה של יחידה
יוצרים ומקצים יחידה שתייצג מכונה אזורית של השירות שמקבלת הגדרות של feature flag. מכיוון שהתבנית הרגילה ב-App Design Center ריקה, ההקצאה מפעילה סימולציה של תשתית ללא פעולה, בלי לפרוס משאבי ענן, ומעבירה את היחידה למצב מוכן כדי ש-saasconfig.googleapis.com יוכל להציג את הגדרת ה-feature flag:
במסוף Google Cloud , עוברים אל App Lifecycle Manager > Units.
לוחצים על יצירה.
בשדה Unit name, מזינים
backend-service-unit.ברשימה SaaS offering, בוחרים באפשרות
flags-quickstart-offering.ברשימה Region, בוחרים באפשרות
us-central1.ברשימה Unit kind, בוחרים את סוג היחידה של
backend-service-template-שנוצר בקטע הקודם.לוחצים על יצירה. בסיום היצירה, נפתח הדף פרטי היחידה.
בדף Unit details, לוחצים על Provision כדי לפתוח את החלונית Provision unit.
ברשימה Release, בוחרים את הגרסה שנוצרה לסוג היחידה
backend-service-template-.ברשימה Service account, בוחרים את חשבון השירות לסימולציה, ואז בוחרים באפשרות Grant App Lifecycle Manager permission to act on behalf of the actuation service account.
לוחצים על Add tenant project variables ובוחרים את הפרויקט כדי לאכלס את
tenant_project_idו-tenant_project_number.לוחצים על Provision וממתינים עד שהערך של State בדף Unit details יהיה Ready.
יצירה ופריסה של feature flag במסוף
מגדירים feature flag בוליאני ומריצים השקה כדי להעביר את גרסת הדגל ליחידה:
- במסוף Google Cloud , פותחים את App Lifecycle Manager > Feature flags ולוחצים על Create feature flag.
- בשדה Unit kind, בוחרים את סוג היחידה של
backend-service-template-. - בשדה Flag key, מזינים את הערך
enable-new-feature(השדה Flag id מתמלא באופן אוטומטי אחרי שמזינים את מפתח הדגל). - מגדירים את Flag value לערך
enabledולוחצים על Create. - עוברים אל App Lifecycle Manager > Flag releases ולוחצים על Create.
- בשדה Flag release name מזינים
enable-new-feature-rel-1, ברשימה Unit kind בוחרים את סוג היחידהbackend-service-template-, ברשימה Release type בוחרים באפשרות Feature flag, ברשימה Select feature flags בוחרים את הדגלenable-new-featureולוחצים על Create. App Lifecycle Manager יוצר באופן אוטומטי את גרסת הדגל הבסיסית. - עוברים אל App Lifecycle Manager > Rollout kinds ולוחצים על Create.
- בשדה Rollout kind name, מזינים
flags-quickstart-rollout-kind. - ברשימה Unit Kind, בוחרים את סוג היחידה
backend-service-template-. - ברשימה Rollout Strategy, בוחרים באפשרות All locations at once.
- לוחצים על יצירה.
- עוברים אל App Lifecycle Manager > Rollouts, ולוחצים על Create.
- בוחרים באפשרות השקת feature flag.
- בשדה Rollout name, מזינים
flags-quickstart-rollout. - ברשימה Unit Kind, בוחרים את סוג היחידה
backend-service-template-. - ברשימה Feature flags release, בוחרים באפשרות
enable-new-feature-rel-1ולוחצים על Next. - בוחרים באפשרות Select existing rollout kind, ואז בוחרים באפשרות
flags-quickstart-rollout-kindברשימה Rollout Kind. - לוחצים על Create כדי להתחיל את ההשקה ולהעביר את ההגדרות אל
backend-service-unit. - מחכים עד שהסטטוס של ההשקה יהיה Succeeded.
הערכת ה-feature flag
בודקים את הדגל enable-new-feature באופן מקומי באמצעות OpenFeature SDK והספק flagd שמחובר ל-saasconfig.googleapis.com:443:
מתקינים את חבילות Python הנדרשות:
pip install --upgrade google-auth grpcio requests "openfeature-sdk>=0.8.2" "openfeature-provider-flagd>=0.6.1"יוצרים קובץ בשם
evaluate_flag.pyעם הקוד הבא:import os import time import google.auth import google.auth.transport.grpc import google.auth.transport.requests import grpc from openfeature import api from openfeature.contrib.provider.flagd import FlagdProvider from openfeature.contrib.provider.flagd.config import ResolverType provider_id = os.environ["FLAGD_SOURCE_PROVIDER_ID"] credentials, _ = google.auth.default( scopes=["https://www.googleapis.com/auth/cloud-platform"] ) channel_creds = grpc.composite_channel_credentials( grpc.ssl_channel_credentials(), grpc.metadata_call_credentials( google.auth.transport.grpc.AuthMetadataPlugin( credentials, google.auth.transport.requests.Request() ) ), grpc.metadata_call_credentials( lambda ctx, cb: cb( [("x-goog-request-params", f"name={provider_id}")], None ) ), ) api.set_provider( FlagdProvider( resolver_type=ResolverType.IN_PROCESS, host="saasconfig.googleapis.com", port=443, sync_metadata_disabled=True, provider_id=provider_id, channel_credentials=channel_creds, ) ) time.sleep(5) value = api.get_client().get_boolean_value("enable-new-feature", False) print(f"Flag 'enable-new-feature' evaluated to: {value}")מגדירים את משתנה הסביבה
FLAGD_SOURCE_PROVIDER_IDלנתיב המשאב של הגדרת ה-feature flag של היחידה, ומריצים את הסקריפט:export FLAGD_SOURCE_PROVIDER_ID="projects/PROJECT_ID/locations/us-central1/featureFlagsConfigs/backend-service-unit" python evaluate_flag.pyמחליפים את
PROJECT_IDבמזהה הפרויקט ב- Google Cloud.הסקריפט מפיק את הפלט
Flag 'enable-new-feature' evaluated to: True.
הסרת המשאבים
כדי לא לצבור חיובים בחשבון Google Cloud על המשאבים שבהם השתמשתם בדף הזה, מבצעים את הפעולות הבאות.
כדי להימנע מחיובים שוטפים, מחקו את פרויקט הבדיקה בדף Manage resources, או הסירו את המשאבים שיצרתם בסדר הפוך לסדר התלות. כדי לסמן feature flag לניקוי (מצב CLEANUP) ולבטל את הקישור שלו לסוג יחידה, צריך להשתמש ב-CLI של gcloud או ב-App Lifecycle Manager API:
מגדירים את מצב הדגל לערך
cleanup, יוצרים גרסת דגל לניקוי ומריצים השקה של ניקוי כדי לבטל את הקישור של הדגל לסוג היחידה:gcloud beta app-lifecycle-manager flags update FLAG_ID \ --project=PROJECT_ID \ --location=global \ --state=cleanup gcloud beta app-lifecycle-manager flags releases create enable-new-feature-cleanup-rel \ --project=PROJECT_ID \ --unit-kind=UNIT_KIND_ID \ --flags=FLAG_ID \ --location=global gcloud beta app-lifecycle-manager rollouts create enable-new-feature-cleanup-rollout \ --project=PROJECT_ID \ --flag-release=enable-new-feature-cleanup-rel \ --rollout-kind=flags-quickstart-rollout-kind \ --location=globalמחליפים את מה שכתוב בשדות הבאים:
-
FLAG_ID: המזהה של ה-feature flag (ממולא אוטומטית בתורUNIT_KIND_ID-enable-new-feature). -
PROJECT_ID: מזהה הפרויקט ב- Google Cloud . -
UNIT_KIND_ID: המזהה של סוג היחידה ב-backend-service-template-.
-
אחרי שההשקה של הניקוי מגיעה לסטטוס Succeeded, מוחקים את ההשקות, את סוג ההשקה (
flags-quickstart-rollout-kind) ואת גרסאות הדגלים (enable-new-feature-rel-1ו-enable-new-feature-cleanup-rel) במסוףGoogle Cloud .מחפשים ומוחקים את גרסאות ה-flag שנוצרו אוטומטית, ואז מוחקים את ה-feature flag:
gcloud beta app-lifecycle-manager flags revisions list \ --project=PROJECT_ID \ --location=global \ --filter="flag:FLAG_ID" gcloud beta app-lifecycle-manager flags revisions delete REVISION_ID \ --project=PROJECT_ID \ --location=globalמחליפים את
REVISION_IDבכל אחד ממזהי הגרסאות שהוחזרו על ידי הפקודהlist, ואז מוחקים את ה-feature flag במסוףGoogle Cloud .מבטלים את ההקצאה של
backend-service-unitבדף Unit details (לוחצים על Deprovision, מאשרים בתיבת הדו-שיח וממתינים עד שהסטטוס יהיה Not provisioned). לאחר מכן מוחקים את שאר המשאבים בסדר הפוך של תלות: היחידה (backend-service-unit), שיוך ברירת המחדל של גרסת סוג היחידה וגרסת תוכנית הפעולה שנוצרה (מנקים את גרסת ברירת המחדל בסוג היחידה לפני שמוחקים את גרסת תוכנית הפעולה), סוג היחידה והמוצר ב-SaaS (flags-quickstart-offering). למידע על כללי המחיקה של App Lifecycle Manager, קראו את המאמר מחיקת משאבים.מוחקים את
flags-composite-templateואתbackend-service-templateב-App Design Center.
המאמרים הבאים
- סקירה כללית על דגלי התכונות של App Lifecycle Manager
- כדי לפרוס באמצעות ניהול תשתית, נסו את המדריך למתחילים בנושא פריסת דגלים של תכונות.
- מגדירים חלוקת תנועה לפי אחוזים בשימוש בהשקות הדרגתיות של תכונות.
- פתרון בעיות ב-feature flags: אבחון בעיות בהערכה או בהשקה של feature flags.